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

时间:2017-11-23 22:29:05

标签: python python-2.7 tkinter

我已经编码多年了,直到现在我一直都能在Google上找到答案。我无法以任何方式完成这项工作。

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

我使用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()

3 个答案:

答案 0 :(得分:1)

只需替换例如:

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

使用:

Label(master, text="FXCM does not accept cards issued in %s" % x).pack()

在你的每个职能中。

或者更好的补充:

lbl = Label(master)
lbl.pack()
在您的e行下

,然后将return替换为:

lbl['text'] = x

答案 1 :(得分:0)

您不会调用textBox函数。为此,如果我正确理解问题,被调用的函数必须更新文本框标签。此外,您不能将lower()县发送到该功能。缩短版代码

xargs grep ...

答案 2 :(得分:0)

# 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')