使用 tkinter 分配全局变量并销毁 gui
Using tkinter to assign a global variable and destroy the gui
我一直在编写年度数据验证程序,需要一些用户输入,因此决定采用 tkinter 路线。我已经为其中一个用户输入屏幕创建了界面,并且还必须创建其他界面,但是在创建 selection 之后我遇到了一些关于 windows 被破坏的问题,并且变量的全球化。
所以理想情况下程序是运行,弹出window,一个适当的属性selection被创建,那个按钮上的文本被传递给"assign" 函数,它创建一个要在我的程序中使用的全局变量,window 消失。
就目前而言,此代码的 运行ning 导致错误:"TclError: can't invoke "button" command: application has been destroyed"。
如果我注释掉 "mGui.destroy()" 行,我可以 select 一个按钮并手动关闭 window,但是 "DRN" 变量被传递给变量 "x" 不管怎样!
import sys
from Tkinter import *
def assign(value):
global x
x = value
mGui.destroy()
mGui = Tk()
mGui.geometry("500x100+500+300")
mGui.title("Attribute Selection Window")
mLabel = Label(mGui, text = "Please select one of the following attributes to assign to the selected Convwks feature:").pack()
mButton = Button(mGui, text = "CON", command = assign("CON")).pack()
mButton = Button(mGui, text = "MS", command = assign("MS")).pack()
mButton = Button(mGui, text = "DRN", command = assign("DRN")).pack()
mGui.mainloop() #FOR WINDOWS ONLY
额外问题:将所有按钮放在同一行中,中间留有空格,同时保持它们居中。
您的代码存在的问题是您在添加按钮命令时无法调用函数。不能写Button(command=function())
,必须写Button(command=function)
。如果你想将一个参数传递给一个函数,你必须这样做:
而不是:
mButton = Button(mGui, text = "CON", command = assign("CON")).pack()
mButton = Button(mGui, text = "MS", command = assign("MS")).pack()
mButton = Button(mGui, text = "DRN", command = assign("DRN")).pack()
你必须写:
mButton = Button(mGui, text = "CON", command = lambda: assign("CON")).pack()
mButton = Button(mGui, text = "MS", command = lambda: assign("MS")).pack()
mButton = Button(mGui, text = "DRN", command = lambda: assign("DRN")).pack()
如果你想把所有的按钮放在同一行,你可以使用这个代码:
import sys
from Tkinter import *
def assign(value):
global x
x = value
mGui.destroy()
mGui = Tk()
mGui.geometry("500x100+500+300")
mGui.title("Attribute Selection Window")
frame1 = Frame(mGui)
frame1.pack()
mLabel = Label(frame1, text = "Please select one of the following attributes to assign to the selected Convwks feature:").grid(row=0, column=0)
frame2 = Frame(mGui)
frame2.pack()
mButton = Button(frame2, text = "CON", command = lambda: assign("CON")).grid(row=0, column=0, padx=10)
mButton = Button(frame2, text = "MS", command = lambda: assign("MS")).grid(row=0, column=1, padx=10)
mButton = Button(frame2, text = "DRN", command = lambda: assign("DRN")).grid(row=0, column=2, padx=10)
mGui.mainloop() #FOR WINDOWS ONLY
我一直在编写年度数据验证程序,需要一些用户输入,因此决定采用 tkinter 路线。我已经为其中一个用户输入屏幕创建了界面,并且还必须创建其他界面,但是在创建 selection 之后我遇到了一些关于 windows 被破坏的问题,并且变量的全球化。
所以理想情况下程序是运行,弹出window,一个适当的属性selection被创建,那个按钮上的文本被传递给"assign" 函数,它创建一个要在我的程序中使用的全局变量,window 消失。
就目前而言,此代码的 运行ning 导致错误:"TclError: can't invoke "button" command: application has been destroyed"。
如果我注释掉 "mGui.destroy()" 行,我可以 select 一个按钮并手动关闭 window,但是 "DRN" 变量被传递给变量 "x" 不管怎样!
import sys
from Tkinter import *
def assign(value):
global x
x = value
mGui.destroy()
mGui = Tk()
mGui.geometry("500x100+500+300")
mGui.title("Attribute Selection Window")
mLabel = Label(mGui, text = "Please select one of the following attributes to assign to the selected Convwks feature:").pack()
mButton = Button(mGui, text = "CON", command = assign("CON")).pack()
mButton = Button(mGui, text = "MS", command = assign("MS")).pack()
mButton = Button(mGui, text = "DRN", command = assign("DRN")).pack()
mGui.mainloop() #FOR WINDOWS ONLY
额外问题:将所有按钮放在同一行中,中间留有空格,同时保持它们居中。
您的代码存在的问题是您在添加按钮命令时无法调用函数。不能写Button(command=function())
,必须写Button(command=function)
。如果你想将一个参数传递给一个函数,你必须这样做:
而不是:
mButton = Button(mGui, text = "CON", command = assign("CON")).pack()
mButton = Button(mGui, text = "MS", command = assign("MS")).pack()
mButton = Button(mGui, text = "DRN", command = assign("DRN")).pack()
你必须写:
mButton = Button(mGui, text = "CON", command = lambda: assign("CON")).pack()
mButton = Button(mGui, text = "MS", command = lambda: assign("MS")).pack()
mButton = Button(mGui, text = "DRN", command = lambda: assign("DRN")).pack()
如果你想把所有的按钮放在同一行,你可以使用这个代码:
import sys
from Tkinter import *
def assign(value):
global x
x = value
mGui.destroy()
mGui = Tk()
mGui.geometry("500x100+500+300")
mGui.title("Attribute Selection Window")
frame1 = Frame(mGui)
frame1.pack()
mLabel = Label(frame1, text = "Please select one of the following attributes to assign to the selected Convwks feature:").grid(row=0, column=0)
frame2 = Frame(mGui)
frame2.pack()
mButton = Button(frame2, text = "CON", command = lambda: assign("CON")).grid(row=0, column=0, padx=10)
mButton = Button(frame2, text = "MS", command = lambda: assign("MS")).grid(row=0, column=1, padx=10)
mButton = Button(frame2, text = "DRN", command = lambda: assign("DRN")).grid(row=0, column=2, padx=10)
mGui.mainloop() #FOR WINDOWS ONLY