我对在python中制作GUI非常陌生,我到处都找过,但找不到我想要的东西。我只想有一个程序,用户输入一个半径,然后程序在点击或输入按钮的帮助下打印出标签中该半径的区域。提前感谢:)
import Tkinter, math
class calc_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
self.entryVariable = Tkinter.DoubleVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=0,row=0,sticky='EW')
self.entry.bind("<Return>", self.OnPressEnter)
self.entryVariable.set(u"Radius")
button = Tkinter.Button(self,text=u"Click to convert",command=self.OnButtonClick)
button.grid(column=1,row=0)
self.labelVariable = Tkinter.StringVar()
label = Tkinter.Label(self,textvariable=self.labelVariable,anchor="w",fg="white",bg="gray")
label.grid(column=0,row=1,columnspan=2,sticky='EW')
self.labelVariable.set(u"Please enter a radius.")
self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnButtonClick(self):
self.labelVariable.set( self.convertVariable.get() )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnPressEnter(self,event):
self.converter()
self.labelVariable.set( self.convertVariable.get() )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def converter(self):
self.convertVariable(math.pi * self.entryVariable.get()**2)
if __name__ == "__main__":
app = calc_tk(None)
app.title('Radius Converter')
app.mainloop()
我已经稍微修改了您的代码。现在,代码将显示您提到的标签上的区域。检查代码。如果我错了,请告诉我。
import Tkinter, math
class calc_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=0,row=0,sticky='EW')
self.entry.bind("<Return>", self.OnPressEnter)
self.entryVariable.set(u"Radius")
button = Tkinter.Button(self,text=u"Click to convert",command=self.OnButtonClick)
button.grid(column=1,row=0)
self.labelVariable = Tkinter.StringVar()
label = Tkinter.Label(self,textvariable=self.labelVariable,anchor="w",fg="white",bg="gray")
label.grid(column=0,row=1,columnspan=2,sticky='EW')
self.labelVariable.set(u"Please enter a radius.")
self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnButtonClick(self):
self.labelVariable.set( str(self.converter())+" is the area." )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnPressEnter(self,event):
self.labelVariable.set( str(self.converter())+" is the area." )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def converter(self):
print self.entryVariable.get()
return (math.pi * (float(self.entryVariable.get()) * float(self.entryVariable.get())))
if __name__ == "__main__":
app = calc_tk(None)
app.title('Radius Converter')
app.mainloop()