获取文件路径并将其显示在顶级小部件的条目上不起作用
Getting filepath and showing it on entry of toplevel widget is not working
顶层的功能是为我的程序获取配置信息。
在顶层,有一个获取文件路径的按钮和一个显示文件路径的条目。
但文件路径未显示在条目中。
还有配置按钮,如果我点击它,然后正确输入填充信息,那么 Toplevel 应该被销毁,但它不能正常工作。
global MDR_DIR
def configureBtn(initWin):
EMAIL_REGEX = re.compile(r"^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$")
USERNAME = username.get()
USERCODE = usercode.get()
USERMAIL = useremail.get()
UF_PATH = MDR_DIR.get()
if not USERNAME:
messagebox.showwarning("Warning", "Username should not be empty")
elif not USERNAME:
messagebox.showwarning("Warning", "Usercode should not be empty")
elif not USERMAIL:
messagebox.showwarning("Warning", "User email should not be empty")
elif not EMAIL_REGEX.match(USERMAIL):
messagebox.showwarning("Warning", "User email address is not valid")
elif not validate_email(USERMAIL):
messagebox.showwarning("Warning", "User email address does not exist")
elif not UF_PATH:
messagebox.showwarning("Warning", "User directory should not be empty")
else :
makeCfgFile(USERNAME, USERCODE, USERMAIL, UF_PATH)
initWin.destroy()
def browseButton():
filename = filedialog.askdirectory(
title="Select working directory")
MDR_DIR.set(filename)
print(filename)
def configMain():
if not os.path.exists('./lpConverter'):
os.mkdir('./lpConverter')
initWin = tkinter.Toplevel()
initWin.title('LPCoverter_v.1.0.0 Configuration')
lb_Username = tkinter.Label(initWin, text = "Username")
username = tkinter.StringVar()
lb_Usercode = tkinter.Label(initWin, text = "Usercode")
usercode = tkinter.StringVar()
lb_UserEmail = tkinter.Label(initWin, text = "Email")
useremail = tkinter.StringVar()
lb_UserDir = tkinter.Label(initWin, text= "MedDRA_DIR")
MDR_DIR = tkinter.StringVar()
lb_UserDirShow = tkinter.Entry(initWin, text="./", textvariable=MDR_DIR)
lb_UserDirShow.insert(0, './')
entry_Username = tkinter.Entry(initWin, textvariable=username)
entry_Username.insert(0, 'Username')
entry_Usercode = tkinter.Entry(initWin, textvariable=usercode)
entry_Usercode.insert(0, 'Usercode')
entry_UserEmail = tkinter.Entry(initWin, textvariable=useremail)
btn_UserMDir = tkinter.Button(initWin, text="..." , command=browseButton)
lb_Username.grid(row=0, sticky='E')
lb_Usercode.grid(row=1, sticky='E')
lb_UserEmail.grid(row=2, sticky='E')
lb_UserDir.grid(row=3, sticky='E')
lb_UserDirShow.grid(row=3, column=1)
entry_Username.grid(row=0, column=1)
entry_Usercode.grid(row=1, column=1)
entry_UserEmail.grid(row=2, column=1)
btn_UserMDir.grid(row=3, column=2)
cfgButton = tkinter.Button(initWin, text="Configure", command=configureBtn)
cfgButton.grid(row=5, column=1)
def lltptMain():
print("Enter")
if os.path.isfile(FNAME):
line3 = readCfgFile()
if lines is not None :
mdrFilepath = lines[3]
today = date.today()
if line3 is not None:
root.filename = filedialog.askopenfilename(initialdir=mdrFilepath, title="Select MDR Code", filetypes=(("EXCEL files", "*.xlsx"), ("CSV files", "*.csv"), ("ASCII files", "*.asc")))
mdrFileLoc = root.filename
if root.filename is not None:
lltptConverter()
root = tkinter.Tk()
root.title('LPCoverter_v.1.0.0')
setCfgBtn = tkinter.Button(root, text="Select Configure", command=configMain)
doConvBtn = tkinter.Button(root, text="Converter", command=lltptMain)
setCfgBtn.pack()
doConvBtn.pack()
root.mainloop()
使用以下代码创建配置按钮以删除 window 创建的
cfgButton = tkinter.Button(initWin, text="Configure", command=lambda: configureBtn(initWin))
cfgButton.grid(row=5, column=1)
这里使用lambda
可以让函数得到main window.
的parameter
顶层的功能是为我的程序获取配置信息。 在顶层,有一个获取文件路径的按钮和一个显示文件路径的条目。 但文件路径未显示在条目中。 还有配置按钮,如果我点击它,然后正确输入填充信息,那么 Toplevel 应该被销毁,但它不能正常工作。
global MDR_DIR
def configureBtn(initWin):
EMAIL_REGEX = re.compile(r"^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$")
USERNAME = username.get()
USERCODE = usercode.get()
USERMAIL = useremail.get()
UF_PATH = MDR_DIR.get()
if not USERNAME:
messagebox.showwarning("Warning", "Username should not be empty")
elif not USERNAME:
messagebox.showwarning("Warning", "Usercode should not be empty")
elif not USERMAIL:
messagebox.showwarning("Warning", "User email should not be empty")
elif not EMAIL_REGEX.match(USERMAIL):
messagebox.showwarning("Warning", "User email address is not valid")
elif not validate_email(USERMAIL):
messagebox.showwarning("Warning", "User email address does not exist")
elif not UF_PATH:
messagebox.showwarning("Warning", "User directory should not be empty")
else :
makeCfgFile(USERNAME, USERCODE, USERMAIL, UF_PATH)
initWin.destroy()
def browseButton():
filename = filedialog.askdirectory(
title="Select working directory")
MDR_DIR.set(filename)
print(filename)
def configMain():
if not os.path.exists('./lpConverter'):
os.mkdir('./lpConverter')
initWin = tkinter.Toplevel()
initWin.title('LPCoverter_v.1.0.0 Configuration')
lb_Username = tkinter.Label(initWin, text = "Username")
username = tkinter.StringVar()
lb_Usercode = tkinter.Label(initWin, text = "Usercode")
usercode = tkinter.StringVar()
lb_UserEmail = tkinter.Label(initWin, text = "Email")
useremail = tkinter.StringVar()
lb_UserDir = tkinter.Label(initWin, text= "MedDRA_DIR")
MDR_DIR = tkinter.StringVar()
lb_UserDirShow = tkinter.Entry(initWin, text="./", textvariable=MDR_DIR)
lb_UserDirShow.insert(0, './')
entry_Username = tkinter.Entry(initWin, textvariable=username)
entry_Username.insert(0, 'Username')
entry_Usercode = tkinter.Entry(initWin, textvariable=usercode)
entry_Usercode.insert(0, 'Usercode')
entry_UserEmail = tkinter.Entry(initWin, textvariable=useremail)
btn_UserMDir = tkinter.Button(initWin, text="..." , command=browseButton)
lb_Username.grid(row=0, sticky='E')
lb_Usercode.grid(row=1, sticky='E')
lb_UserEmail.grid(row=2, sticky='E')
lb_UserDir.grid(row=3, sticky='E')
lb_UserDirShow.grid(row=3, column=1)
entry_Username.grid(row=0, column=1)
entry_Usercode.grid(row=1, column=1)
entry_UserEmail.grid(row=2, column=1)
btn_UserMDir.grid(row=3, column=2)
cfgButton = tkinter.Button(initWin, text="Configure", command=configureBtn)
cfgButton.grid(row=5, column=1)
def lltptMain():
print("Enter")
if os.path.isfile(FNAME):
line3 = readCfgFile()
if lines is not None :
mdrFilepath = lines[3]
today = date.today()
if line3 is not None:
root.filename = filedialog.askopenfilename(initialdir=mdrFilepath, title="Select MDR Code", filetypes=(("EXCEL files", "*.xlsx"), ("CSV files", "*.csv"), ("ASCII files", "*.asc")))
mdrFileLoc = root.filename
if root.filename is not None:
lltptConverter()
root = tkinter.Tk()
root.title('LPCoverter_v.1.0.0')
setCfgBtn = tkinter.Button(root, text="Select Configure", command=configMain)
doConvBtn = tkinter.Button(root, text="Converter", command=lltptMain)
setCfgBtn.pack()
doConvBtn.pack()
root.mainloop()
使用以下代码创建配置按钮以删除 window 创建的
cfgButton = tkinter.Button(initWin, text="Configure", command=lambda: configureBtn(initWin))
cfgButton.grid(row=5, column=1)
这里使用lambda
可以让函数得到main window.
parameter