使用Python3的tkinter制作一个简单的计算器界面

1 #!/usr/bin/python3

 2 #-*-coding:utf-8-*-

 3 import tkinter

 4 import tkinter.font

 5

 6 class Calculate():

 7     def __init__(self):

 8          self.tk=tkinter.Tk();

 9          self.tk.title("Calculate");

10

11         self.tk.resizable(0,0);

12         self.showfont=tkinter.font.Font(self.tk,size=26);

13         self.sysfont=tkinter.font.Font(self.tk,size=16);

14

15         self.entry=tkinter.Entry(self.tk,width=20,font=self.showfont,bac    kground="#ffffff");

16         self.entry.grid(row=0,column=0,columnspan=4,pady=10);

17

18         self.btn1=tkinter.Button(self.tk,text="1",font=self.sysfont);

19         self.btn1.grid(row=1,column=0,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

20

21         self.btn2=tkinter.Button(self.tk,text="2",font=self.sysfont);

22         self.btn2.grid(row=1,column=1,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

23

24         self.btn3=tkinter.Button(self.tk,text="3",font=self.sysfont);

25         self.btn3.grid(row=1,column=2,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

26

27         self.btn_divide=tkinter.Button(self.tk,text="÷",font=self.sysfon    t);

28         self.btn_divide.grid(row=1,column=3,sticky=tkinter.N+tkinter.S+t    kinter.W+tkinter.E);

29

30

31         self.btn4=tkinter.Button(self.tk,text="4",font=self.sysfont);

32         self.btn4.grid(row=2,column=0,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

33

34         self.btn5=tkinter.Button(self.tk,text="5",font=self.sysfont);

35         self.btn5.grid(row=2,column=1,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

36

37         self.btn6=tkinter.Button(self.tk,text="6",font=self.sysfont);

38         self.btn6.grid(row=2,column=2,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

39

40         self.btn_mult=tkinter.Button(self.tk,text="×",font=self.sysfont)    ;

41         self.btn_mult.grid(row=2,column=3,sticky=tkinter.N+tkinter.S+tki    nter.W+tkinter.E);

42

43

44         self.btn7=tkinter.Button(self.tk,text="7",font=self.sysfont);

45         self.btn7.grid(row=3,column=0,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

46

47         self.btn8=tkinter.Button(self.tk,text="8",font=self.sysfont);

48         self.btn8.grid(row=3,column=1,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

49

50         self.btn9=tkinter.Button(self.tk,text="9",font=self.sysfont);

51         self.btn9.grid(row=3,column=2,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

52

53         self.btn_minus=tkinter.Button(self.tk,text="-",font=self.sysfont    );

54         self.btn_minus.grid(row=3,column=3,sticky=tkinter.N+tkinter.S+tk    inter.W+tkinter.E);

55

56

57         self.btn0=tkinter.Button(self.tk,text="0",font=self.sysfont);

58         self.btn0.grid(row=4,column=0,sticky=tkinter.N+tkinter.S+tkinter    .W+tkinter.E);

59

60         self.btn_point=tkinter.Button(self.tk,text=".",font=self.sysfont    );

61         self.btn_point.grid(row=4,column=1,sticky=tkinter.N+tkinter.S+tk    inter.W+tkinter.E);

62

63         self.btn_es=tkinter.Button(self.tk,text="=",font=self.sysfont);

64         self.btn_es.grid(row=4,column=2,sticky=tkinter.N+tkinter.S+tkint    er.W+tkinter.E);

65

66         self.btn_add=tkinter.Button(self.tk,text="+",font=self.sysfont);

67         self.btn_add.grid(row=4,column=3,sticky=tkinter.N+tkinter.S+tkin    ter.W+tkinter.E);

68

69         self.tk.mainloop();

70

71 cal=Calculate();

 

你可能感兴趣的:(python3)