2013-04-04 35 views
1

我一直在試圖使用python在tkinter中設計一個表單,但不幸的是我再次卡住了,但現在把一個表單放入我已經擁有的筆記本中。我已經分別運行它們,並且它們工作得很完美,當我嘗試將它們放在一起時,問題就開始了。例如,如果我寫入「componentComb = ttk.Combobox(frameOne,width =」19「)」而不是frameOne,我把firstStep這是我想做的事情(合併它們),我有一個錯誤這樣的:是否可以使用tkinter和python將表單放入筆記本小部件中?

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

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") 
+0

親愛twasbrillig,感謝您的答覆。我已經試過你說的到底是什麼,它不起作用,有另一種方式來解決這個問題..?提前致謝! – Hector 2013-04-05 05:54:59

+0

它不工作如何?你是否收到相同的錯誤信息? – twasbrillig 2013-04-05 06:02:19

+0

其實它什麼都不做,它只是停止運行,當我嘗試運行代碼時沒有任何反應......然後,我必須強制退出python,因爲它停止嘗試運行......任何想法? – Hector 2013-04-05 06:28:34

相關問題