问题背景:
尝试建一个GUI计算器,所以尝试了封装.封装之前,实现了t_in - t_out 为结论。封装之后,直接tk界面不显示了。
以下为run.py
from tkinter import *
from tkinter import ttk
from settings import Settings
from value import calc
root = Tk()
cal_settings = Settings()
root.title(cal_settings.title)
root.geometry(cal_settings.geometry)
root.configure(bg="#484848")
runner = calc(root)
root.mainloop()
value.py
from tkinter import *
class calc(object):
def __init__(self, root):
super(calc, self).__init__()
self.root = root
self.inEntry = Entry(self.root,width=5,relief="groove")
self.inEntry.grid(row=0,column=0,padx=20,pady=20)
self.outEntry = Entry(self.root,width=5,relief="groove")
self.outEntry.grid(row=1,column=0,padx=20,pady=20)
self.res_calc()
def delta_t_calc(self):
self.t_in = float(self.inEntry.get())
self.t_out = float(self.outEntry.get())
self.delta_t = self.t_in - self.t_out
return print(self.delta_t)
def res_calc(self):
self.res = Button(self.root,text='calculate \n now',
command=self.delta_t_calc(),relief="groove",bg="#393939",fg="white")
self.res.grid(row=2,column=0)
运行结果及报错
Traceback (most recent call last):
File "/home/sharon/Heat Loss Calculator/run.py", line 12, in <module>
runner = calc(root)
File "/home/sharon/Heat Loss Calculator/value.py", line 11, in __init__
self.res_calc()
File "/home/sharon/Heat Loss Calculator/value.py", line 21, in res_calc
command=self.delta_t_calc(),relief="groove",bg="#393939",fg="white")
File "/home/sharon/Heat Loss Calculator/value.py", line 14, in delta_t_calc
self.t_in = float(self.inEntry.get())
ValueError: could not convert string to float: ''
[Finished in 294ms]
我的尝试方法
把导致运算运行的button单独拿出来,具体不知道
我想达到的结果
请问如何才能达到封装之前一样的效果呢?