这是mainProgram():
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)
我要求用户在一个框中输入一个国家/地区,我有几个不同国家/地区的列表,并检查用户输入是否与每个列表中的任何国家/地区匹配。如果是的话,我运行一个单独的函数来打印一行。
这是主要的tkinter代码
master = Tk()
e = Entry(master)
e.pack()
e.focus_set()
def clear_text():
#WHAT TO ENTER HERE???
b = Button(master, text = "Search", bg="lightyellow", command = mainProgram)
b.pack()
c = Button(master, text="Quit", bg="orange",
command=master.quit)
c.pack()
d = Button(master, text="Clear", bg="orange",
command=clear_text)
d.pack()
mainloop()
在此之前,我有几个不同国家/地区的列表,以及十几个仅为用户打印内容的功能,这与此无关。
我使用tkinter创建一个输入框,用户可以在其中输入国家(输入分配给typedCountry)和三个按钮:搜索,清除和退出。搜索运行mainProgram()函数,返回的函数将打印到tkinter框。但是,由于我不确切知道它的打印位置,因此我不知道如何告诉“清除”按钮删除文本框中的内容而不会完全破坏它。
当我搜索说美国时,这就是它的样子:
https://i.stack.imgur.com/mPCBm.jpg
然后我搜索奥地利:
https://i.stack.imgur.com/omJPV.jpg
正如您所看到的,一张印在另一张下面,但我需要将它们从包装盒中清除。由于我不知道tkinter在哪里打印这个,所以无论我做什么,我都无法清除它。
如果有帮助,这是主要代码
# -*- coding: utf-8 -*-
from tkinter import *
import tkMessageBox
import os
allCountries = ['afghanistan','aland','albania','algeria','american samoa','andorra',
'angola','anguilla','argentina','armenia','aruba','australia','austria','bahamas',
'bahrain','bangladesh','barbados','belarus','belgium','belize','benin','bermuda',
'bhutan','bolivia','bonaire','bosnia and herzegovina','botswana','brazil','brunei',
'bulgaria','burkina faso','burma','burundi','cambodia','cameroon','cape verde',
'cayman islands','central african republic','chad','colombia','comoros','congo',
'cook islands','costa rica','cote d\'ivoire','crimea','croatia','cuba','cyprus',
'czech republic','denmark','djibouti','dominican republic','ecuador','egypt',
'el salvador','equatorial guinea','eritrea','estonia','ethiopia','falkland islands (malvinas)',
'faroe islands','finland','france','gabon','gambia','germany','ghana','greece',
'greenland','grenada','guam','guatemala','guinea','guinea-bissau','guyana',
'haiti','hungary','iceland','india','iran','iraq','ireland','italy','jamaica',
'japan','jordan','kazakhstan','kenya','kiribati','kosovo','kuwait','kyrgyzstan',
'laos','latvia','lebanon','lesotho','liberia','libya','liechtenstein','lithuania',
'luxembourg','macao','macedonia','madagascar','malawi','malaysia','maldives',
'mali','malta','marshall islands','martinique','mauritania','mauritius','mayotte',
'mexico','micronesia','moldova','monaco','mongolia','montenegro','montserrat',
'morocco','mozambique','myanmar','namibia','nauru','nepal','netherlands','new zealand',
'nicaragua','niger','nigeria','north korea','northern mariana islands','norway',
'oman','pakistan','palau','panama','papua new guinea','paraguay','peru','philippines',
'poland','portugal','puerto rico','qatar','romania','russia','rwanda','saint helena',
'saint kitts and nevis','saint lucia','saint vincent and the grenadines','samoa',
'sao tome and principe','saudi arabia','senegal','serbia','seychelles','sierra leone',
'singapore','slovakia','slovenia','solomon islands','somalia','south africa','south korea',
'spain','sri lanka','sudan','suriname','swaziland','sweden','switzerland','syria',
'taiwan','tajikistan','tanzania','thailand','togo','tokelau','tonga','trinidad and tobago',
'tunisia','turkey','turkmenistan','turks and caicos islands','tuvalu','uganda','ukraine',
'united kingdom','united states','uruguay','uzbekistan','vanuatu','venezuela',
'virgin islands, u.s.','western sahara','yemen','zambia','zimbabwe','palestine'
]
bannedCountries = ['afghanistan','american samoa','belarus','brazil','burundi',
'central african republic','crimea','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','south sudan','syria','tokelau','turkey','ukraine','united states','vanuatu','virgin islands',
'western sahara','zimbabwe', 'timor-leste']
cpBannedCountries = ['venezuela','united states','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",'ireland']
eftCountries = ['australia']
sepaCountries = ['austria','belgium','bulgaria','croatia','cyprus','czech republic','check',
'denmark','estonia','finland','france','french guiana','germany','greece','gibraltar',
'hungary','iceland','guadeloupe'
'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',
'colombia','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','palestine']
ibanCountries = [
'lebanon', 'albania','andorra','austria', 'azerbaijan','bahrain','belgium','bosnia and herzegovina',
'brazil','bulgaria','croatia','cyprus','czech republic','denmark', "estonia",'faroe islands',
'finland','france','french guiana','germany','gibraltar','greece','guadeloupe','guernsey','hungary',
'iceland','ireland','isle of man','israel','italy','jersey','jordan','kuwait','latvia','liechtenstein',
'lithuania','luxembourg','macedonia','malta','martinique','mauritania','mauritius','mayotte',
'moldova','monaco','montenegro','netherlands','norway','pakistan','poland','portugal',
'qatar','reunion','romania','san marino','saudi arabia','serbia','slovakia','slovenia','spain',
'sweden','switzerland','united kingdom','saint barthelemy','saint martin','saint pierre and miquelon',
'tunisia','united arab emirates','kosovo'
]
noLPOACountries = ['austria', 'belgium','bulgaria','croatia','cyprus', 'czech republic',
'denmark', "estonia",'finland','france','germany','greece','hungary','iceland','ireland',
'italy','latvia','liechtenstein','lithuania','luxembourg','malta','netherlands',
'norway','poland','portugal','romania','slovakia','slovenia','spain','sweden','switzerland']
max50cpCountries = ['france','germany','greece','italy']
zuluBannedCountries = ['italy']
idchecklistCountries =['kenya']
nonidchecklistCountries = ['jordan','kuwait','oman','qatar','saudi arabia',
'united arab emirates']
def banned(x):
if 'kingdom' not in x:
Label(master, text="""Clients from %s cannot apply for a live account with FXCM.""" % x).pack()
else:
return
def cpBanned(x):
Label(master, text="""Clients from %s cannot use credit/debit cards.""" % x).pack()
def cpAllowed(x):
Label(master, text="Clients from %s can deposit with credit cards." % x).pack()
def skrillBanned(x):
Label(master, text="Clients from %s cannot use Skrill" % x).pack()
def Bacs(x):
Label(master, text="""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.\nClients from %s can
apply for a Spreadbetting account.""" % (x, x, x)).pack()
def sepa(x):
Label(master, text="""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.
Clients from %s can have a maximum leverage of 200:1.""" % (x, x, x)).pack()
def eft(x):
Label(master, text="""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.\nClients in %s must open with FXCM AU""" % (x, x, x)).pack()
def ltd(x):
Label(master, text="Clients from %s must open with FXCM LTD" % x).pack()
def markets(x):
Label(master, text="Clients from %s must open with FXCM Markets" % x).pack()
def noLPOA(x):
Label(master, text="""All traders (MMs, IBs, family members, etc.) domiciled
within %s and/or managing funds for residents of %s must be regulated.
Managers acquiring clients across %s must have the appropriate passport or license
to do so.""" % (x, x, x)).pack()
def iban(x):
lbl['text'] = "IBAN is required for all wire withdrawals with a beneficiary bank account located in %s." % x
def max50cp(x):
Label(master, text="Residents of %s are NOT eligible for the ATG 50K CP deposit maximum. " % x).pack()
def zulu(x):
Label(master, text="Residents of %s cannot use ZuluTrader. " % x).pack()
def idchecklist(x):
Label(master, text="""For applicants residing in %s who have difficulty
submitting a valid proof of residence, we can accept the ID checklist
as an alternative. In addition, these clients will only be required
to submit one photo ID and P.O. box proof of residence with the ID
Checklist in lieu of two photo IDs.
""" % x).pack()
def nonidchecklist(x):
Label(master, text="""For applicants from %s who have difficulty submitting
a valid proof of residence and reside in Kuwait, they are not required to
send in an ID checklist or extra ID when submitting a POR listing a P.O.
Box as long as the city/state matches the application. If the city/ state
does not match, the ID checklist is required to satisfy the account opening
requirements
""" % x).pack()
def notFound(x):
Label(master, text="Country %s was not found, please try again" % x).pack()
def printAsterix():
Label(master, text="*"*40).pack()
def mainProgram():
typedCountry = e.get() # This is the text you may want to use later
if not typedCountry:
return
lowerCountry = typedCountry.lower()
upperCountry = typedCountry.title()
if lowerCountry in bannedCountries: #banned countries
banned(upperCountry)
if lowerCountry in cpBannedCountries:#cp banned countries
cpBanned(upperCountry)
if lowerCountry in skrillBannedCountries: #skrill banned countries
skrillBanned(upperCountry)
if lowerCountry not in cpBannedCountries and lowerCountry not in bannedCountries and lowerCountry in allCountries:
cpAllowed(upperCountry) #cp allowed countries
if lowerCountry in sepaCountries: #sepa allowed countries (EEA)
sepa(upperCountry)
if lowerCountry in eftCountries: #eft allowed countries (AU)
eft(upperCountry)
if lowerCountry in ltdCountries: #LTD required
ltd(upperCountry)
if lowerCountry in marketsCountries: #markets required
markets(upperCountry)
if lowerCountry in noLPOACountries: #lpoa not available
noLPOA(upperCountry)
if lowerCountry in ibanCountries: #iban required
iban(upperCountry)
if lowerCountry in max50cpCountries:
max50cp(upperCountry)
if lowerCountry in zuluBannedCountries:
zulu(upperCountry)
if lowerCountry in idchecklistCountries:
idchecklist(upperCountry)
if lowerCountry in nonidchecklistCountries:
nonidchecklist(upperCountry)
if lowerCountry not in allCountries: #not found
notFound(typedCountry)
printAsterix()
master = Tk()
e = Entry(master)
e.pack()
e.focus_set()
def clear_text():
lbl.withdraw()
b = Button(master, text = "Search", bg="lightyellow", command = mainProgram)
b.pack()
c = Button(master, text="Quit", bg="orange",
command=master.quit)
c.pack()
d = Button(master, text="Clear", bg="orange",
command=clear_text)
d.pack()
mainloop()
答案 0 :(得分:0)
您的代码会在每次成功运行查询时创建一个标签,该标签没有分配名称,因此您无法专门解压缩它。标签也是在主窗口的master
框内创建的,而不是在框架内部,这是您应该考虑的事项。要从窗口中解压缩所有以前的标签,您应该使用:
slaves = master.pack_slaves()
for s in slaves:
s.pack_forget()
然后重新打包你需要保留的东西。
e.pack()
b.pack()
c.pack()
d.pack()
每个按钮。但是,清除屏幕上的内容后,文本输入框不会为空。因此,如果您愿意,也可以清除它。
foo = StringVar()
e = Entry(master, textvariable=foo)
然后在清除时,添加:
foo.set("")
希望这有帮助! GL