是否可以使用tkinter和python将表单放入笔记本小部件中?
我一直在尝试使用python在tkinter中设计表单,但不幸的是,我又被卡住了,但现在将表单放入我已有的笔记本中。我已经分别运行了它们,它们工作得很好,当我尝试将它们组合在一起时,问题就开始了 例如,如果我在“componentComb=ttk.Combobox(frameOne,width=“19”)”而不是frameOne中写入我想做的第一步(合并它们),我会出现如下错误:是否可以使用tkinter和python将表单放入笔记本小部件中?,python,forms,tkinter,Python,Forms,Tkinter,我一直在尝试使用python在tkinter中设计表单,但不幸的是,我又被卡住了,但现在将表单放入我已有的笔记本中。我已经分别运行了它们,它们工作得很好,当我尝试将它们组合在一起时,问题就开始了 例如,如果我在“componentComb=ttk.Combobox(frameOne,width=“19”)”而不是frameOne中写入我想做的第一步(合并它们),我会出现如下错误: componentComb= ttk.Combobox(firstStep, width="19") NameEr
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()
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")
亲爱的twasbrillig,感谢您的回复。。。我完全按照你说的去做了,但是没有用,有没有别的办法解决这个问题?谢谢!它不起作用了到底是怎么回事?您是否收到相同的错误消息?实际上它什么都不做,只是停止运行,当我尝试运行代码时,什么都没有发生。。。然后,我必须强制退出python,因为它停止尝试运行。。。有什么想法吗?嗯,我只是用“名称错误:名称‘第一步’未定义”来解决这个问题。这听起来像是另一个问题;我建议把它作为一个单独的问题发布。很抱歉