我是Python新手,目前正在学习Python中的GUI tkinter,我受到井字棋游戏代码的启发,并想如果我可以用它制作计算器会怎么样,所以我尝试并实现了我尽了我最大的能力编写了一个非常好的计算器,但我真的不知道如何添加水平方向的滚动条或使方程文本随着表达式变大而变小,就像在移动计算器中一样,所以我需要你的帮助它还评价我的计算器代码,比如我可以改进什么?以及我不应该做的事情。
这是我的全部代码-->
import tkinter as tk
from tkinter import *
initial_expression = ''
def onclick(row, column):
value = buttons[row][column]['text']
global initial_expression
initial_expression = initial_expression + value
equation.set(initial_expression)
def clear():
equation.set(' ')
global initial_expression
initial_expression = ''
def solve():
try :
global initial_expression
evaluate_expression = str(eval(initial_expression))
equation.set(evaluate_expression)
initial_expression = evaluate_expression
except:
equation.set('Error')
initial_expression = ''
def delete_last_string():
try:
global initial_expression
element_list = []
element_index = 0
for element in initial_expression:
element_list = element_list + [element]
element_index += 1
final_index = element_index - 1
element_list.remove(element_list[final_index])
new_element = ''
for element in element_list:
new_element = new_element + element
initial_expression = new_element
equation.set(initial_expression)
except:
equation.set(' ')
window = tk.Tk()
window.title('Calculator')
equation = StringVar()
equation.set('')
button_text = [['+','-','*','/'],
['1','2','3','C'],
['4','5','6','='],
['7','8','9','.'],
['0','(',')','<-']]
buttons = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
operators = ['+','-','*','/','=','C','.','(',')','<-']
expression_label = Label(window, textvariable = equation, font = ('consolas',50))
expression_label.pack(side = 'top', pady = 10, padx = 10)
frame = Frame(window)
frame.pack(padx = 10, pady = 10)
for row in range(5):
for column in range(4):
buttons[row][column] = Button(frame, width = 5, height = 1, font = ('consolas',30),
text = button_text[row][column])
buttons[row][column].grid(row = row, column = column)
for operator in button_text[row][column]:
if operator in operators:
buttons[row][column].config(bg = 'orange', activebackground = 'brown',
activeforeground = 'white')
if buttons[row][column]['text'] != 'C' and buttons[row][column]['text'] !=
'=' and buttons[row][column]['text'] != '<-':
buttons[row][column].config(command = lambda row = row, column = column :
onclick(row, column))
elif buttons[row][column]['text'] == 'C':
buttons[row][column].config(command = clear)
elif buttons[row][column]['text'] == '=':
buttons[row][column].config(command = solve)
elif buttons[row][column]['text'] == '<-':
buttons[row][column].config(command = delete_last_string)
else:
buttons[row][column].config(bg = 'lightgrey', activebackground = 'grey')
buttons[row][column].config(command = lambda row = row, column = column :
onclick(row,column))
window.mainloop()
一种解决方案是将
expression_label
从 tk.Label
更改为 tk.Entry
小部件,如下所示。
expression_label = Entry(window, textvariable = equation, width = 13, font = ('consolas',50))
如果您愿意,您还可以添加水平滚动条,就像这样。
scroll = tk.Scrollbar(window, orient = tk.HORIZONTAL, command = expression_label.xview)
scroll.pack(fill="both")
expression_label.config(xscrollcommand = scroll.set)