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

Python 将函数结果打印到tkinter消息框中?,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我已经写了很多年了,我一直都能在谷歌上找到答案,直到现在。我无论如何也做不到这一点 在此之前,我有几个不同国家的列表,以及十几个只为用户打印内容的函数,这里不相关 我使用tkinter创建一个输入框,用户可以在其中键入国家(输入被分配给typedCountry)。然后,我在mainProgram()函数的每个列表中搜索国家,每次找到匹配的列表时,我都返回另一个函数。除了我希望mainProgram()将信息返回到tkinter GUI框而不是终端之外,一切都正常工作。我已经和它斗争了好几个小时,

我已经写了很多年了,我一直都能在谷歌上找到答案,直到现在。我无论如何也做不到这一点

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

我使用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()
下面是您需要的主要代码(例如,如果您希望在您的终端运行此代码):


例如,只需更换:

from tkinter import *
import tkMessageBox



bannedCountries = ['afghanistan','american samoa','belarus','brazil','burundi',
                    'central african republic','congo','cook islands','cote d\'ivoire',
                    'crimea','cuba','guam','iran','japan','liberia','libya','myanmar',
                    'new zealand','north korea','northern mariana islands','puerto rico',
                    'russia','singapore','somalia','south korea','sudan','syria','tokelau',
                    'turkey','ukraine','united states','vanuatu','virgin islands',
                    'western sahara','zimbabwe']

cpBannedCountries = ['belarus','bosnia and herzegovina','burundi','cote d\'ivoire',
'cuba','iran','iraq','kosovo','lebanon','liberia','macedonia','montenegro','myanmar',
'nigeria','north korea','saint helena','somalia','sudan']

skrillBannedCountries = ['angola','barbados','benin','burkina faso','cape verde',
'comoros','djibouti','faroe islands','gambia','greenland','grenada','guyana','laos',
'liechtenstein','macao','martinique','mongolia','namibia','niger','palau','samoa',
'suriname','tajikistan','togo','trinidad and tobago','turkmenistan']

bacsCountries = ["united kingdom"]

eftCountries = ['australia']

sepaCountries = ['austria','belgium','bulgaria','cyprus','czech republic','check',
'denmark','estonia','finland','france','germany','greece','hungary','iceland',
'ireland','italy','latvia','liechtenstein','lithuania','luxembourg','malta',
'martinique','mayotte','monaco','netherlands','norway','poland','portugal',
'romania','slovakia','slovenia','spain','sweden','switzerland','united kingdom']

ltdCountries = ['austria','belgium','bulgaria','croatia','cyprus','czech republic',
'denmark','estonia','finland','france','germany','greece','hungary','ireland',
'italy','latvia','lithuania','luxembourg','malta','netherlands','poland','portugal',
'romania','slovakia','slovenia','spain','united kingdom']

marketsCountries = ['albania','algeria','andorra','angola','anguilla','armenia',
'aruba','bahamas','bangladesh','barbados','belize','benin','bermuda','bhutan',
'bonaire','bosnia','herzegovina','bosnia and herzegovina','botswana','brunei',
'burkina faso','burma','cambodia','cameroon','cape verde','cayman islands',
'chad','comoros','djibouti','equatorial guinea','eritrea','ethiopia','falkland islands (malvinas)',
'faroe islands','gabon','gambia','ghana','greenland','grenada','guinea','guinea-bissau',
'guyana','haiti','iceland','india','jamaica','jordan','kazakhstan','kenya',
'kiribati','kosovo','kyrgyzstan','laos','lesotho','liechtenstein','macao',
'macedonia','madagascar','malawi','malaysia','maldives','mali','marshall islands',
'mauritania','mauritius','micronesia','mongolia','morocco','mozambique','namibia',
'nauru','nepal','niger','nigeria','norway','pakistan','palau','papua new guinea',
'philippines','rwanda','saint helena','saint kitts and nevis','saint lucia','saint vincent and the grenadines',
'samoa','sao tome and principe','senegal','serbia','seychelles','sierra leone',
'solomon islands','sri lanka','suriname','swaziland','tajikistan','tanzania','togo',
'tonga','trinidad and tobago','tunisia','turkmenistan','turks and caicos islands','tuvalu',
'uganda','uzbekistan','yemen','zambia']

def banned(x):
    if 'kingdom' not in x:
        return 'Clients from %s are not able to open an account with FXCM' % x
    else:
        return

def cpBanned(x):
    return "FXCM does not accept cards issued in %s" % x

def skrillBanned(x):
    return "Clients from %s cannot use Skrill" % x

def Bacs(x):
    return """Clients from %s can use BACS if their bank account is located in
%s and both their bank account and their FXCM account is in GBP""" % (x, x)

def sepa(x):
    return """Clients from %s can use SEPA if their bank account is located either in
%s or in another European country, and both their bank account and their FXCM account is in EUR""" % (x, x)

def eft(x):
    return """Clients from %s can use EFT if their bank account is located in
%s, and both their bank account and their FXCM account is in AUD""" % (x, x)
    print "Clients from %s must open with FXCM AU" % x

def ltd(x):
    return "Clients from %s must open with FXCM LTD" % x

def markets(x):
    return "Clients from %s must open with FXCM Markets" % x

def mainProgram():
    typedCountry = e.get() # This is the text you may want to use later
    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()
return "FXCM does not accept cards issued in %s" % x
与:

在您的每个功能中


或者最好加上:

lbl = Label(master)
lbl.pack()
e
行下,然后将
return
s替换为:

lbl['text'] = x

您不调用textBox函数。如果我正确理解了这个问题,那么调用的函数必须更新文本框标签。此外,您不需要将lower()country发送给函数。代码的缩短版本

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()
导入系统 如果系统版本信息[0]<3: 将Tkinter作为tk##Python 2.x导入 其他: 将tkinter作为tk##Python 3.x导入 LTD国家=[“奥地利”、“比利时”、“保加利亚”、“克罗地亚”、“塞浦路斯”、“捷克共和国”] SEPA国家=[‘奥地利’、‘比利时’、‘保加利亚’、‘塞浦路斯’、‘捷克共和国’、‘支票’] 市场中心=[‘阿尔巴尼亚’、‘阿尔及利亚’、‘安道尔’、‘安哥拉’、‘安圭拉’] def有限公司(国家): 变量集(变量get()+“***有限公司”+国家/地区) def sepa(国家): 变量集(变量get()+“***sepa”+国家) def市场(国家/地区): 变量集(变量get()+“***市场”+国家/地区) def main程序(): typedCountry=e.get() 打印('您的国家是:'+typedCountry+'\n') country\u lower=类型country.lower() 对于国家/地区列表,国家/地区功能位于((ltdCountries,ltd), (国家环保总局, (市场中心、市场)): 如果国家/地区列表中的国家/地区较低: 国家/地区功能(国家/地区较低) master=tk.tk() e=传统入口(主入口) e、 包() e、 焦点集() var=tk.StringVar() 变量集(“”) 标签(master,textvariable=var,bg=“lightyellow”, 宽度=25) b=tk.按钮(master,text=“Search”,command=main程序) b、 包() 按钮(主控,text=“退出”,bg=“橙色”, command=master.quit).pack() master.mainloop()
tkinter
应该只使用一个
Tk()
来创建主窗口。其他窗口(子窗口、对话框、消息框)应使用
Toplevel()
tkinter
应该只使用一个
mainloop()
来控制所有的窗口和小部件-如果你有两个mainloop,那么它可能在
get()
set()
上有问题。顺便说一句:
tkinter
有标准的消息框:你知道每次“点击”后消息消失的方法吗?因为现在他们只是停留在那里,每次点击都会添加一个新的呼叫function@JosephHarari第二部分应该是每次调用函数时更新一个标签。我一直在玩弄你说的,你是对的,但这意味着只打印最后一个函数。如果一个国家/地区匹配了两个或多个定义,则标签不会显示所有定义,而是只显示最新的一个谢谢,但是我刚刚测试了此解决方案,它也在执行相同的操作(只是将信息返回到终端)。我想要的是,当用户点击“搜索”时,运行函数main程序,结果以guit显示给用户。当输入缩写列表中的国家时,标签在我的系统(Linux Salckware)上正确更新。如果它不能在您的系统上工作,那么这是一个操作系统特有的问题,我无法帮助您解决。
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')