2011-05-17 95 views
3

我創建的以下應用程序用於演示tkinter中的多個窗口。主要的問題是沒有一個Entry控件(在bmi計算器或轉換器中)不接受輸入框中的值 - 當我進行計算時,它們會得到一個ValueError。ttk tkinter python3多個框架/窗口

 
from tkinter import * 
from tkinter import ttk 
from tkinter import messagebox 

class App1(ttk.Frame): 
    def createWidgets(self): 
     #text variables 
     self.i_height = StringVar() 
     self.i_weight = StringVar() 
     self.o_bmi = StringVar() 

     #labels 
     self.label1 = ttk.Label(self, text="Enter your weight:").grid(row=0, column=0, sticky=W) 
     self.label2 = ttk.Label(self, text="Enter your height:").grid(row=1, column=0, sticky=W) 
     self.label3 = ttk.Label(self, text="Your BMI is:").grid(row=2, column=0, sticky=W) 

     #text boxes 
     self.textbox1 = ttk.Entry(self, textvariable=self.i_weight).grid(row=0, column=1, sticky=E) 
     self.textbox2 = ttk.Entry(self, textvariable=self.i_height).grid(row=1, column=1, sticky=E) 
     self.textbox3 = ttk.Entry(self, textvariable=self.o_bmi).grid(row=2, column=1, sticky=E) 

     #buttons 
     self.button1 = ttk.Button(self, text="Cancel/Quit", command=self.quit).grid(row=3, column=1, sticky=E) 
     self.button1 = ttk.Button(self, text="Ok", command=self.calculateBmi).grid(row=3, column=2, sticky=E) 

    def calculateBmi(self): 
     try: 
      self.weight = float(self.i_weight.get()) 
      self.height = float(self.i_height.get()) 
      self.bmi = self.weight/self.height ** 2.0 
      self.o_bmi.set(self.bmi) 
     except ValueError: 
      messagebox.showinfo("Error", "You can only use numbers.") 
     finally: 
      self.i_weight.set("") 
      self.i_height.set("") 

    def __init__(self, master=None): 
     ttk.Frame.__init__(self, master) 
     self.grid() 
     self.createWidgets() 

class App2(ttk.Frame): 
    def create_widgets(self): 
     """Create the widgets for the GUI""" 
     #1 textbox (stringvar) 
     self.entry= StringVar() 
     self.textBox1= ttk.Entry(self, textvariable=self.entry).grid(row=0, column=1) 

     #5 labels (3 static, 1 stringvar) 
     self.displayLabel1 = ttk.Label(self, text="feet").grid(row=0, column=2, sticky=W) 
     self.displayLabel2 = ttk.Label(self, text="is equivalent to:").grid(row=1, column=0) 
     self.result= StringVar() 
     self.displayLabel3 = ttk.Label(self, textvariable=self.result).grid(row=1, column=1) 
     self.displayLabel4 = ttk.Label(self, text="meters").grid(row=1, column=2, sticky=W) 

     #2 buttons 
     self.quitButton = ttk.Button(self, text="Quit", command=self.quit).grid(row=2, column=1, sticky=(S,E)) 
     self.calculateButton = ttk.Button(self, text="Calculate", command=self.convert_feet_to_meters).grid(row=2, column=2, sticky=(S,E)) 

    def convert_feet_to_meters(self): 
     """Converts feet to meters, uses string vars and converts them to floats""" 
     self.measurement = float(self.entry.get()) 
     self.meters = self.measurement * 0.3048 
     self.result.set(self.meters) 

    def __init__(self, master=None): 
     ttk.Frame.__init__(self, master) 
     self.grid() 
     self.create_widgets() 

def button1_click(): 
    root = Tk() 
    app = App1(master=root) 
    app.mainloop() 

def button2_click(): 
    root = Tk() 
    app = App2(master=root) 
    app.mainloop() 

def main(): 
    window = Tk() 
    button1 = ttk.Button(window, text="bmi calc", command=button1_click).grid(row=0, column=1) 
    button2 = ttk.Button(window, text="feet conv", command=button2_click).grid(row=1, column=1) 
    window.mainloop() 

if __name__ == '__main__': 
    main() 

我該如何解決這個問題,但仍然保持類結構和python3的使用?任何幫助將不勝感激。謝謝。 順便說一句 - 類似於C#的form1.Show()?

回答

9

你必須在STRINGVAR()轉換爲整數/浮點或使用IntVar()或DoubleVar()

還有其他的問題。聲明類似下面的回報「無」,因爲它是一個網格()對象,而不是標籤()對象:

self.label1 = ttk.Label(self, text="Enter your weight:").grid(row=0, column=0, sticky=W) 

我想你也將有兩個TK()窗口同時打開的問題。改用Toplevel()或分開框架。

+0

格不是對象,也不返回一個對象。這只是一個功能。但是你是正確的,Label(...)。grid(...)不返回任何有用的東西。 – 2011-05-18 00:36:43

+0

它僅用於佈局,它是在函數create_widgets中設置框架中的小部件。我使用它作爲一個類,因爲它有助於在一個地方設置所有的小部件,並且我喜歡單獨的襯墊:) – 2011-05-19 11:35:20

+0

@py_tk_coder:如果是這種情況,請不要將結果分配給實例變量。這是點,混亂,並使您的「單一內襯」超過必要的時間。 – 2011-05-19 12:48:37

4

感謝喬,你幫了很多忙,我偶然發現了答案...使用ttk你可以使用筆記本,並且由於我的兩個類都是派生幀,所以我可以將它們添加到筆記本的幀中...

def main(): 
#Setup Tk() 
window = Tk() 

#Setup the notebook (tabs) 
notebook = ttk.Notebook(window) 
frame1 = ttk.Frame(notebook) 
frame2 = ttk.Frame(notebook) 
notebook.add(frame1, text="BMI Calc") 
notebook.add(frame2, text="Feet to Meters") 
notebook.grid() 

#Create tab frames 
app1 = App1(master=frame1) 
app1.grid() 
app2 = App2(master=frame2) 
app2.grid() 

#Main loop 
window.mainloop() 

這解決了問題:)