【发布时间】:2020-02-16 18:10:17
【问题描述】:
我正在尝试使用 Tkinter Python 开发一个小部件。它应该分两个阶段将数据作为用户的输入,然后根据给定的输入进一步进行。我的代码根本没有给出任何输出。我是 Python 新手。
这是我的代码:
from tkinter import *
def f_step():
global txt_1
global txt_2
global v0
window=Tk()
txt_1=StringVar()
Label(window,text="DB/rst name (without extension)").place(x=10,y=75)
Entry(window,textvariable=txt_1,width=40).place(x=225,y=75)
txt_2=StringVar()
Label(window,text="DB/rst folder path").place(x=10,y=100)
Entry(window,textvariable=txt_2,width=40).place(x=225,y=100)
v0=StringVar()
#v0.set(1)
Label(window,text="Do you have first node number").place(x=10,y=150)
Radiobutton(window,text="yes",variable=v0,value="a").place(x=225,y=150)
Radiobutton(window,text="no",variable=v0,value="b").place(x=350,y=150)
Button(window,text="Next",command=clicked).place(x=250,y=250)
window.title('Path operation application')
window.geometry("500x300+10+10")
window.mainloop()
def clicked():
global db_name
global fpath
global aa
db_name = txt_1.get()
fpath = txt_2.get()
aa=v0.get()
window.destroy()
Initiaterun()
def Initiaterun():
if aa == "a": # Checks to see if you entered the correct data.
r = Tk() # Opens new window
r.title(':D')
r.geometry('150x50') # Makes the window a certain size
rlbl = Label(r, text='\n[+] with node number operation') # "logged in" label
rlbl.pack() # Pack is like .grid(), just different
r.mainloop()
elif aa == "b":
r = Tk()
r.title('D:')
r.geometry('150x50')
rlbl = Label(r, text='\n[!] With component file operation')
rlbl.pack()
r.mainloop()
def DelUser():
r.destroy() # Destroys the login window
f_step() # And goes back to the start!
'''
【问题讨论】:
-
首先你必须运行
DelUser() -
当我运行
DelUser()或f_step()时,您的代码只会给我错误 - 您使用了一些变量(如f或window),您不会将其作为参数发送给代码或你使用局部变量window,它不能在其他函数中访问它来销毁它。也许使用类来保持在一起并使用self.而不是global。但首先在控制台中运行它以查看是否收到错误消息 - 输出任何内容可能是您的主要问题。 -
顺便说一句:你期望什么输出?您不使用任何
print()来显示输出。如果我选择yes或no,它只会显示带有一些标签的窗口。如果我不选择yes或no,那么我不会得到窗口。创建我们可以运行的最小工作代码。