计算器软件页面设置
- 1、一个经典的面向程序写法模块
- 2、布局管理器代码(grid)
- 3、源代码
1、一个经典的面向程序写法模块
from tkinter import *
from tkinter import messagebox
class Appication(Frame):
"""
一个经典的 GUI 类的写法
"""
def __init__(self, master = None):
super().__init__(master)
self.master = master
self.pack()
self.CreateWidget()
def CreateWidget(self):
"""
用途:通过 grid 布局实现计算器的界面
"""
......
......
if __name__ == "__main__":
window = Tk()
window.geometry("200x200+200+200")
window.title('计算机界面')
app = Appication(master = window)
window.mainloop()
2、布局管理器代码(grid)
def CreateWidget(self):
"""
用途:通过 grid 布局实现计算器的界面
"""
buttonsText = (('MC', 'M+', 'M-', 'MR'),
('C', '±', '/', 'x'),
('7', '8', '9', '-'),
('4', '5', '6', '+'),
('1', '2', '3', '='),
('0', '.'))
Entry(self).grid(row = 0, column = 0, columnspan = 4)
for rindex, r in enumerate(buttonsText):
for cindex, c in enumerate(r):
if c == '=':
Button(self, text = c, width = 2)\
.grid(row = rindex + 1, column = cindex, rowspan = 2, sticky = NSEW)
elif c == 0:
Button(self, text = c, width = 2) \
.grid(row = rindex + 1, column = cindex, columnspan = 2, sticky = NSEW)
elif c == '.':
Button(self, text = c, width = 2) \
.grid(row = rindex + 1, column = cindex + 1, sticky = NSEW)
else:
Button(self, text = c, width = 2) \
.grid(row = rindex + 1, column = cindex, sticky = NSEW)
3、源代码
from tkinter import *
from tkinter import messagebox
import random
class Appication(Frame):
"""
一个经典的 GUI 类的写法
"""
def __init__(self, master = None):
super().__init__(master)
self.master = master
self.pack()
self.CreateWidget()
def CreateWidget(self):
"""
用途:通过 grid 布局实现计算器的界面
"""
buttonsText = (('MC', 'M+', 'M-', 'MR'),
('C', '±', '/', 'x'),
('7', '8', '9', '-'),
('4', '5', '6', '+'),
('1', '2', '3', '='),
('0', '.'))
Entry(self).grid(row = 0, column = 0, columnspan = 4)
for rindex, r in enumerate(buttonsText):
for cindex, c in enumerate(r):
if c == '=':
Button(self, text = c, width = 2)\
.grid(row = rindex + 1, column = cindex, rowspan = 2, sticky = NSEW)
elif c == 0:
Button(self, text = c, width = 2) \
.grid(row = rindex + 1, column = cindex, columnspan = 2, sticky = NSEW)
elif c == '.':
Button(self, text = c, width = 2) \
.grid(row = rindex + 1, column = cindex + 1, sticky = NSEW)
else:
Button(self, text = c, width = 2) \
.grid(row = rindex + 1, column = cindex, sticky = NSEW)
if __name__ == "__main__":
window = Tk()
window.geometry("200x200+100+100")
window.title('计算机界面')
app = Appication(master = window)
window.mainloop()