我一直在尝试使用python在tkinter中设置一个表单,但不幸的是我再次陷入困境,但现在将一个表单放入我已经拥有的笔记本中。我已经单独运行它们并且它们工作完美,当我尝试将它们放在一起时问题就开始了。
例如,如果我写“componentComb = ttk.Combobox(frameOne,width =”19“)”而不是frameOne我把firstStep放在我想做的事情(合并它们),我有这样的错误:
componentComb= ttk.Combobox(firstStep, width="19")
NameError:名称'firstStep'未定义
我不明白,我已经定义了,但可能错了!!!你能帮我解决这个问题吗?
下面你有我一直在“打架”的代码,希望你能帮助我!
提前致谢
这是我的代码:
#Starts
import Tkinter
from Tkinter import *
from ttk import *
import tkMessageBox
import ttk
# start of GUI code
root = Tk()
root.title("Model A")
root.minsize(1000, 150)
root.maxsize(1100, 200)
notebook = ttk.Notebook(root)
notebook.pack(fill='both', expand='yes')
notebook.pressed_index = None
# Child Frame
frameOne = Tkinter.Frame(notebook, bg='white')
frameOne.pack(fill='both', expand=True)
frameTwo = Tkinter.Frame(notebook, bg='white')
frameTwo.pack(fill='both', expand=True)
frameThree= Tkinter.Frame(notebook, bg='white')
frameThree.pack(fill='both', expand=True)
frameFour= Tkinter.Frame(notebook, bg='white')
frameFour.pack(fill='both', expand=True)
# Pages
notebook.add(frameOne, text='Standard')
notebook.add(frameTwo, text='TID')
notebook.add(frameThree, text='MEE')
notebook.add(frameFour, text='Final')
# Merging form and notebook
def defocus(event):
event.widget.master.focus_set()
if __name__ == '__main__':
firstStep = Tkinter.Label(notebook, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
firstStep.grid(row=2, columnspan=7, sticky='W', \
padx=5, pady=5, ipadx=5, ipady=5)
#Main Selection
componentComb= ttk.Combobox(frameOne, width="19")
componentComb = Combobox(frameOne, state="readonly", values=("TGB", "RST", "CCPa"))
componentComb.grid(column=4, row=0, columnspan="5", sticky="nswe")
componentComb.set("Main Selection")
#Temperature Selection
tempComb = ttk.Combobox(frameOne, width="14")
tempComb = Combobox(frameOne, state="readonly", values=("-40", "-30", "-20","-10", "0", "10","20", "30"))
tempComb.grid(column=0, row=2, columnspan="2", sticky="w")
tempComb.set("Temperature Selection")
#Device Type Selection
DeviceTypeComb = ttk.Combobox(frameOne, width="14")
DeviceTypeComb = Combobox(frameOne, state="readonly", values=("QML", "Non-QML"))
DeviceTypeComb.grid(column=3, row=2, columnspan="2", sticky="w")
DeviceTypeComb.set("Device Type Selection")
#Junction Temperature Selection
JunctionTempComb = ttk.Combobox(frameOne, width="16")
JunctionTempComb = Combobox(frameOne, state="readonly", values=("-40", "-30", "-20","-10", "0", "10","20", "30"))
JunctionTempComb.grid(column=5, row=2, columnspan="2", sticky="w")
JunctionTempComb.set("Junction Temp Selection")
#Chip Area in Cm2 Selection
ChipAreaComb = ttk.Combobox(frameOne, width="12")
ChipAreaComb = Combobox(frameOne, state="readonly", values=("0.001","0.002","0.003","0.004","0.005","0.006"))
ChipAreaComb.grid(column=7, row=2, columnspan="2", sticky="e")
ChipAreaComb.set("Chip Area Selection")
#Time of Exposure
TimeOfExpoComb = ttk.Combobox(frameOne, width="12")
TimeOfExpoComb = Combobox(frameOne, state="readonly", values=("1", "2", "3","4", "5"))
TimeOfExpoComb.grid(column=9, row=2, columnspan="2", sticky="w")
TimeOfExpoComb.set("Time of Exposure")
root.mainloop()
答案 0 :(得分:0)
firstStep
是defocus
函数中的局部变量,您尝试在全局范围内访问它。
您必须在函数之外定义它,以便它在全局范围内具有意义。但请注意,因为您要在函数中分配它,所以您需要使用global
关键字,以便它不会认为您引入了具有相同名称的新局部变量。
这应该有效:
firstStep = None #define the variable globally
def defocus(event):
event.widget.master.focus_set()
if __name__ == '__main__':
global firstStep # we want to reassign the global version of this variable
firstStep = Tkinter.Label(notebook, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
firstStep.grid(row=2, columnspan=7, sticky='W', \
padx=5, pady=5, ipadx=5, ipady=5)
componentComb= ttk.Combobox(firstStep, width="19")