
一个计算器程序
''' @文件 :Caluators.py @说明 : @时间 :2021-04-06 19:43:09 @作者 : @版本 :1.0 '''
import os,sys,time,logging import tkinter from typing import Text logging.basicConfig(level=logging.INFO)
class Caluator(tkinter.Tk): def init(self, master=None): tkinter.Tk.init(self, master) self.caluator_str = [] self.createWidgets()
def createWidgets(self): text_area= tkinter.Text(self) text_area.config(width=120, height=5, bg="white") text_area.pack() numbers_frame = tkinter.Frame(self, width=120, height=10) numbers_frame.pack(ipadx=30, ipady=10) button7 = tkinter.Button(numbers_frame, text="7") button7.config(fOnt="-family {黑体} -size 18") button7.config(width=3, height=1) button7.pack(side="left", padx=10, pady=10) button7.bind("<Button-1>", self.caluator_func(7)) button8 = tkinter.Button(numbers_frame, text="8") button8.config(width=3, height=1) button8.config(fOnt="-family {黑体} -size 18") button8.pack(side="left", padx=10) button9 = tkinter.Button(numbers_frame, text="9") button9.config(width=3, height=1) button9.config(fOnt="-family {黑体} -size 18") button9.pack(side="left", padx=10) buttOnc= tkinter.Button(numbers_frame, text="C") buttonc.config(width=3, height=1) buttonc.config(fOnt="-family {黑体} -size 18") buttonc.pack(side="left", padx=10) numbers_frame1 = tkinter.Frame(self, width=120, height=10) numbers_frame1.pack(ipadx=20, ipady=10) button4 = tkinter.Button(numbers_frame1, text="4") button4.config(width=3, height=1) button4.config(fOnt="-family {黑体} -size 18") button4.pack(side="left", padx=10) button5 = tkinter.Button(numbers_frame1, text="5") button5.config(width=3, height=1) button5.config(fOnt="-family {黑体} -size 18") button5.pack(side="left", padx=10) button6 = tkinter.Button(numbers_frame1, text="6") button6.config(width=3, height=1) button6.config(fOnt="-family {黑体} -size 18") button6.pack(side="left", padx=10) buttOndcr= tkinter.Button(numbers_frame1, text="/") buttondcr.config(width=3, height=1) buttondcr.config(fOnt="-family {黑体} -size 18") buttondcr.pack(side="left", padx=10) numbers_frame2 = tkinter.Frame(self, width=120, height=10) numbers_frame2.pack(ipadx=20, ipady=10) button1 = tkinter.Button(numbers_frame2, text="1") button1.config(width=3, height=1) button1.config(fOnt="-family {黑体} -size 18") button1.pack(side="left", padx=10) button2 = tkinter.Button(numbers_frame2, text="2") button2.config(width=3, height=1) button2.config(fOnt="-family {黑体} -size 18") button2.pack(side="left", padx=10) button3 = tkinter.Button(numbers_frame2, text="3") button3.config(width=3, height=1) button3.config(fOnt="-family {黑体} -size 18") button3.pack(side="left", padx=10) buttOnplus= tkinter.Button(numbers_frame2, text="*") buttonplus.config(width=3, height=1) buttonplus.config(fOnt="-family {黑体} -size 18") buttonplus.pack(side="left", padx=10) numbers_frame3 = tkinter.Frame(self, width=120, height=10) numbers_frame3.pack(ipadx=20, ipady=10) button0 = tkinter.Button(numbers_frame3, text="0") button0.config(width=3, height=1) button0.config(fOnt="-family {黑体} -size 18") button0.pack(side="left", padx=10) buttOnd= tkinter.Button(numbers_frame3, text=".") buttond.config(width=3, height=1) buttond.config(fOnt="-family {黑体} -size 18") buttond.pack(side="left", padx=10) buttOnr= tkinter.Button(numbers_frame3, text="<-") buttonr.config(width=3, height=1) buttonr.config(fOnt="-family {黑体} -size 18") buttonr.pack(side="left", padx=10) buttOne= tkinter.Button(numbers_frame3, text="=") buttone.config(width=3, height=1) buttone.config(fOnt="-family {黑体} -size 18") buttone.pack(side="left", padx=10) def caluator_func(self, numbers): self.caluator_str.append(numbers) print(numbers) if name=="main": app = Caluator() app.title("科学计算器") app.geometry("280x400+120+200") app.mainloop()
直接运行,会输出 7,但是点击 7 这个按钮,不会输出 7,请问是哪里出了问题?
1 no1xsyzy 2021-04-06 21:47:05 +08:00 bind 的第二个参数应是一个函数,且在每次点击按钮时被按下 但你这里是一旦启动会调用一次 self.caluator_func(7),把得到的 None 绑定到按钮上 可以 button7.bind("<Button-1>", functools.partial(self.caluator_func, 7)) 或者 lambda,或者为每个按钮单独设置。 |