求助,关于Python3.4的Tk组件做GUI的问题

 我来答
就烦条0o
2016-07-12 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46492
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
# -*- coding: utf-8 -*-

# please input your code here.
from random import randint
import tkinter
from tkinter import *
from tkinter import messagebox
def main():
root = Tk()# 创建 GUI 主程序
calculator = Label()# 创建主窗口
calculator.pack(fill=BOTH, expand = 1)
addWidgets(calculator) # 调用函数,向窗口上添加功能按钮,以输入数字和运算符
root.title('Calculator')
root.wm_resizable(width = False, height = False) # 禁用窗口缩放
root.mainloop()
def addWidgets(frame):
n1=randint(1,13)
n2=randint(1,13)
n3=randint(1,13)
n4=randint(1,13)
expression = Text(frame, height = 2, width = 28) # 创建表达式输入窗口
number_1 = Button(frame, text=n1, width = 5,command = lambda: input_char(n1, expression))
number_2 = Button(frame, text=n2, width = 5,command = lambda: input_char(n2, expression))
number_3 = Button(frame, text=n3, width = 5,command = lambda: input_char(n3, expression))
number_4 = Button(frame, text=n4, width = 5,command = lambda: input_char(n4, expression))
# 创建数字按钮
btn_add = Button(frame, text = '+', width = 5,command = lambda: input_char('+', expression))
btn_subtract = Button(frame, text = '-', width = 5,command = lambda: input_char('-', expression))
btn_multiply = Button(frame, text = '*', width = 5,command = lambda: input_char('*', expression))
btn_divide = Button(frame, text = '/', width = 5,command = lambda: input_char('/', expression))
btn_equal = Button(frame, text = '=', width = 5,command = lambda: input_char(calcu(expression),expression))# 创建运算符输入按钮
btn_l = Button(frame, text = '(', width = 5,command = lambda: input_char('(', expression))
btn_r = Button(frame, text = ')', width = 5,command = lambda: input_char(')', expression))
btn_delete = Button(frame, text = 'clear', width = 5,command = lambda: expression.delete(0.0,END))
# 使用 grid_configure 函数将组件添加到主窗口上
expression.grid_configure(column = 1, row = 2, columnspan = 4, rowspan = 1)
number_1.grid_configure(column = 1, row = 4)
number_2.grid_configure(column = 2, row = 4)
number_3.grid_configure(column = 3, row = 4)
number_4.grid_configure(column = 4, row = 4)
btn_add.grid_configure(column = 1, row = 7)
btn_subtract.grid_configure(column = 2, row = 7)
btn_multiply.grid_configure(column = 3, row = 7)
btn_divide.grid_configure(column = 4, row = 7)
btn_equal.grid_configure(column = 3, row = 11)
btn_l.grid_configure(column = 1, row = 11)
btn_r.grid_configure(column = 2, row = 11)
btn_delete.grid_configure(column = 4, row = 11)
def input_char(char, expressionview):
expressionview.insert('1.end', char) # 输入按钮对应的字符
def calcu(expressionview):
#计算表达式的值,返回结果的字符串形式。如果表达式不合法,返回错误信息
equal_str = expressionview.get('1.0', '1.end')
expressionview.insert('1.end', '=')
s = []
i = 0
equal_list = []
while i < equal_str.__len__():
if equal_str[i] <= '9' and equal_str[i] >= '0':
tmp = 0
while i < equal_str.__len__() and equal_str[i] <= '9' and equal_str[i] >= '0':
tmp = tmp * 10 + (ord(equal_str[i]) - ord('0'))
i = i + 1
equal_list.append(tmp)
continue
else:
if equal_str[i] == '(':
s.append(equal_str[i])
elif equal_str[i] == ')':
try:
while s[s.__len__()-1] != '(':
equal_list.append(s.pop())
s.pop()
except IndexError:
return 'Invalid expersion!'
elif equal_str[i] == '+' or equal_str[i] == '-':
while s.__len__() > 0 and s[s.__len__()-1] != '(':
equal_list.append(s.pop())
s.append(equal_str[i])
elif equal_str[i] == '*' or equal_str[i] == '/':
while s.__len__() > 0 and (s[s.__len__()-1] == '*' or s[s.__len__() - 1] == '/'):
equal_list.append(s.pop())
s.append(equal_str[i])
i += 1
while s.__len__() > 0:
equal_list.append(s.pop())
# print(equal_list) # print the postfix expression
cnt_num = 0
cnt_op = 0
for item in equal_list:
if type(item) == type(0): # integer.
cnt_num += 1
else:
cnt_op += 1
if cnt_op != cnt_num - 1:
return 'Invalid expersion!'

ans = []
for i in range(0, equal_list.__len__()):
if equal_list[i] == '+':
ans.append(ans.pop(ans.__len__()-2) + ans.pop(ans.__len__()-1))
elif equal_list[i] == '-':
ans.append(ans.pop(ans.__len__()-2) - ans.pop(ans.__len__()-1))
elif equal_list[i] == '*':
ans.append(ans.pop(ans.__len__()-2) * ans.pop(ans.__len__()-1))
elif equal_list[i] == '/':
try:
ans.append(ans.pop(ans.__len__()-2) / ans.pop(ans.__len__()-1))
except ZeroDivisionError:
return 'ZeroDivisionError: division by zero'
else:
ans.append(equal_list[i])
return str(ans[0])
if __name__ == '__main__':
main()
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式