Python 你能帮我解决一下tkinter中的框架只显示一次的问题吗
我只是学习了一些python和tkinter。我有一个问题,我只是想创建一个关于计算器的类,但问题是界面只显示一次。一旦我按下按钮,计算器的界面就消失了。这是代码Python 你能帮我解决一下tkinter中的框架只显示一次的问题吗,python,tkinter,Python,Tkinter,我只是学习了一些python和tkinter。我有一个问题,我只是想创建一个关于计算器的类,但问题是界面只显示一次。一旦我按下按钮,计算器的界面就消失了。这是代码 from tkinter import * from tkinter import messagebox class EasyCalculator: def __init__(self): self.root = Tk() self.s = ''
from tkinter import *
from tkinter import messagebox
class EasyCalculator:
def __init__(self):
self.root = Tk()
self.s = ''
self.Laststring = []
self.easymode = Frame(self.root,height=450,width=320,bg='pink')
self.root.geometry('320x450')
self.root.title('Calculator')
def Calculate(self,string):
result = eval(string)
return result
def renew(self,strings):
self.Laststring.append(self.s)
self.s = self.s + strings
if('<-' in self.s):
self.Laststring = Laststring[:-1]
self.s = self.Laststring[len(self.Laststring)-1]
self.funcpr(self.Laststring[len(self.Laststring)-1])
self.show()
elif('=' not in self.s and 'AC' not in self.s):
try:
self.funcpr(self.s)
except:
messagebox.showinfo('Warning','Oops,something wrong!')
elif('AC' in self.s):
try:
self.s = ''
self.funcpr(' ')
except:
messagebox.showinfo('Warning','Oops,something wrong!')
else:
s2 = self.s.strip('=')
result = self.Calculate(s2)
self.s = self.s + str(result)
self.funcpr(self.s)
self.s=''
def funcpr(self,t):
Label(self.easymode,text = t).grid(row = 0,column=0,columnspan = 4,rowspan = 2)
def CalWindows(self): #interface
Button(self.easymode,text='1',command=lambda:self.renew('1')).place(x = 0,y = 200,width = 80,height = 50)
Button(self.easymode,text='2',command=lambda:self.renew('2')).place(x = 80,y = 200,width = 80,height = 50)
Button(self.easymode,text='3',command=lambda:self.renew('3')).place(x = 160,y = 200,width = 80,height = 50)
Button(self.easymode,text='+',command=lambda:self.renew('+')).place(x = 240,y = 200,width = 80,height = 50)
Button(self.easymode,text='4',command=lambda:self.renew('4')).place(x = 0,y = 250,width = 80,height = 50)
Button(self.easymode,text='5',command=lambda:self.renew('5')).place(x = 80,y = 250,width = 80,height = 50)
Button(self.easymode,text='6',command=lambda:self.renew('6')).place(x = 160,y = 250,width = 80,height = 50)
Button(self.easymode,text='-',command=lambda:self.renew('-')).place(x = 240,y = 250,width = 80,height = 50)
Button(self.easymode,text='7',command=lambda:self.renew('7')).place(x = 0,y = 300,width = 80,height = 50)
Button(self.easymode,text='8',command=lambda:self.renew('8')).place(x = 80,y = 300,width = 80,height = 50)
Button(self.easymode,text='9',command=lambda:self.renew('9')).place(x = 160,y = 300,width = 80,height = 50)
Button(self.easymode,text='X',command=lambda:self.renew('*')).place(x = 240,y = 300,width = 80,height = 50)
Button(self.easymode,text='AC',command=lambda:self.renew('AC')).place(x = 0,y = 350,width = 80,height = 50)
Button(self.easymode,text='<-',command=lambda:self.renew('<-')).place(x = 80,y = 350,width = 80,height = 50)
Button(self.easymode,text='%',command=lambda:self.renew('%')).place(x = 160,y = 350,width = 80,height = 50)
Button(self.easymode,text='/',command=lambda:self.renew('/')).place(x = 240,y = 350,width = 80,height = 50)
Button(self.easymode,text='.',command=lambda:self.renew('.')).place(x = 0,y = 400,width = 80,height = 50)
Button(self.easymode,text='0',command=lambda:self.renew('0')).place(x = 80,y = 400,width = 80,height = 50)
Button(self.easymode,text='=',command=lambda:self.renew('='),bg='Orange').place(x = 160,y = 400,width = 160,height = 50)
def show(self):
self.easymode.pack()
self.CalWindows()
self.root.mainloop()
if __name__=='__main__':
Calculator1 = EasyCalculator()
Calculator1.show()
从tkinter导入*
从tkinter导入消息框
类简易计算器:
定义初始化(自):
self.root=Tk()
self.s=''
self.Laststring=[]
self.easymode=Frame(self.root,高度=450,宽度=320,bg='pink')
self.root.geometry('320x450')
self.root.title('Calculator')
def计算(自身,字符串):
结果=评估(字符串)
返回结果
def续订(自我,字符串):
self.Laststring.append(self.s)
self.s=self.s+字符串
如果在方法funcpr
中()在框架self.easymode
内对标签进行网格化,这将使框架self.easymode
缩小,仅包含标签
您可以通过让frameself.easymode
展开并使用整个包含小部件来防止这种情况:
def show(self):
self.easymode.pack(expand=True, fill='both') # Expand frame
self.CalWindows()
self.root.mainloop()
非常感谢你