import Tkinter
import tkMessageBox
class MyApp(object):
def __init__(self):
self.topFrame = Tkinter.Frame()
self.root = Tk()
self.root.wm_title("My first window!")
self.label = Label(self.root, text="Enter your weight")
self.label.pack(padx=20,pady=10)
self.labeltext = StringVar()
self.labeltext.set("Enter your height!")
Label(self.root, textvariable=self.labeltext).pack()
self.entrytext2 = StringVar()
Entry(self.root, textvariable=self.entrytext2).pack()
self.entrytext2.trace('w', self.entry_changed)
self.buttontext = StringVar()
self.buttontext.set("Bouton")
Button(self.root, textvariable=self.buttontext, command=self.clicked1).pack()
self.entrytext = StringVar()
Entry(self.root, textvariable=self.entrytext).pack()
self.entrytext.trace('w', self.entry_changed)
def calcBMI(self):
weight = float(self.entrytext2.get())
inches = float(self.entrytext.get())
BMI = (weight * 703)/ inches **2
if BMI > 18.5 and BMI < 25:
self.labeltext.set(BMI)
elif BMI < 18.5:
print ' Your BMI is Underweight ', self.value.set(BMI)
else:
print ' Your BMI is Overweight ', self.value.set(BMI)
def clicked1(self):
response = tkMessageBox.askyesno("Confirm", "You clicked?")
if response:
self.labeltext.set("You've been clicked!")
self.buttontext.set("Butttttttton")
print response
MyApp()
所以,當我編輯它,我得到:更新!: Tkinter的Python 2.7版的問題
File "window1.py", line 45, in <module>
MyApp()
File "window1.py", line 7, in __init__
self.root = Tk()
NameError: global name 'Tk' is not defined
你混合了兩件好建議。嘗試'Tkinter.Tk()'在違規行中。 – Junuxx
如果你已經附加到你的問題而不是全部替換它,這將是最好的。現在看起來好像甘達羅和jab9bansai給出了不正確的答案,而他們實際上非常有幫助。 – Junuxx
@ user173314按照Junuxx的建議。 Tk()是模塊Tkinter中的一個類,當你簡單地導入Tkinter時,python需要知道魔術術語Tk()來自哪裏,在這種情況下,它來自你導入的模塊。如果您閱讀Gandaro提供的URL,它將幫助您大大地瞭解這一點。 – jab