1

I have been trying to desing a form in tkinter using python, but unfortunately I am stuck again, but now putting a form into the notebook I already have. I have run them separately and they work perfect, the problem starts when I try to put them together.

For example, if I write in "componentComb= ttk.Combobox(frameOne, width="19")" instead of frameOne I put firstStep which is what I want to do (merge them), I have an error like this:

componentComb= ttk.Combobox(firstStep, width="19")

NameError: name 'firstStep' is not defined

Which I don't understand, I have already defined, but probably wrong!!! Can you help me with this problem?

Below you have the code I have been "fighting" with, and I hope you can help me!

Thanks in advance

Here is my code:

#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()
4

1 回答 1

0

firstStepdefocus函数中的局部变量,您正试图在全局范围内访问它。

您必须在函数之外定义它,以便它在全局范围内具有意义。但请注意,因为您是在函数中分配它,所以您需要使用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")
于 2013-04-05T02:43:43.300 回答