将函数结果打印到tkinter消息框中?

2024-10-06 12:32:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了很多年的代码,直到现在,我总能在谷歌上找到答案。我无论如何也做不到。在

在此之前,我有几个不同国家的列表,以及十几个功能,只为用户打印内容,在这里不相关。在

我使用tkinter创建一个输入框,用户可以在其中键入国家(输入被分配给typedCountry)。然后在mainProgram()函数中的每个列表中搜索国家,每次找到匹配的列表时都返回另一个函数。一切都正常工作,除了我希望mainProgram()将信息返回到tkinter GUI框而不是终端。我已经和它斗争了好几个小时了,我找不到一个让它工作的方法,我愿意接受任何建议,即使是大幅度地修改代码或使用tkinter以外的东西也可以。在

def mainProgram():
    typedCountry = e.get()
    Country = typedCountry.lower()
    print 'Your country is: ' + typedCountry + '\n'
    if Country in bannedCountries:
        banned(typedCountry)
    if Country in cpBannedCountries:
        cpBanned(typedCountry)
    if Country in skrillBannedCountries:
        skrillBanned(typedCountry)
    if Country in bacsCountries:
        Bacs(typedCountry)
    if Country in sepaCountries:
        sepa(typedCountry)
    if Country in eftCountries:
        eft(typedCountry)
    if Country in ltdCountries:
        ltd(typedCountry)
    if Country in marketsCountries:
        markets(typedCountry)

master = Tk()
e = Entry(master)
e.pack()
e.focus_set()
var = mainProgram()

def textBox():
    root = Tk()
    label = Message(root, textvariable=var)
    label.pack()
    root.mainloop()

b = Button(master, text = "Search", command = mainProgram)
b.pack()

mainloop()

如果你想要的话,这里是主要的代码(例如,如果你想在你的终端上运行它):

^{pr2}$

Tags: 函数代码用户inmaster终端列表if
3条回答

只需替换例如:

return "FXCM does not accept cards issued in %s" % x

有:

^{pr2}$

在你的每个功能中。在


或者最好加上:

lbl = Label(master)
lbl.pack()

e行下,然后将return替换为:

lbl['text'] = x

你不能调用textBox函数。要使其正常工作,如果我正确地理解了问题,被调用的函数必须更新文本框标签。不要把函数发送给下级。代码的缩短版本

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

ltdCountries = ['austria','belgium','bulgaria','croatia','cyprus','czech republic']
sepaCountries = ['austria','belgium','bulgaria','cyprus','czech republic','check']
marketsCountries = ['albania','algeria','andorra','angola','anguilla']

def ltd(country):
    var.set(var.get() +" *** ltd " + country)

def sepa(country):
    var.set(var.get() +" *** sepa " + country)

def markets(country):
    var.set(var.get() +" *** markets " + country)

def mainProgram():
    typedCountry = e.get()
    print('Your country is: ' + typedCountry + '\n')
    country_lower=typedCountry.lower()
    for country_list, country_function in ((ltdCountries, ltd),
                                           (sepaCountries, sepa),
                                           (marketsCountries, markets)):
        if country_lower in country_list:
            country_function(country_lower)

master = tk.Tk()
e = tk.Entry(master)
e.pack()
e.focus_set()

var=tk.StringVar()
var.set("")
tk.Label(master, textvariable=var, bg="lightyellow",
         width=25).pack()

b = tk.Button(master, text = "Search", command = mainProgram)
b.pack()
tk.Button(master, text="Quit", bg="orange",
          command=master.quit).pack()
master.mainloop()
# First create a text box
txt = scrolledtext.ScrolledText(root)
# this line is for deleting it's content
txt.delete(1.0, END)
# this other line is for inserting text in it
txt.insert(1.0, 'Some Text')

相关问题 更多 >