【发布时间】:2013-11-23 12:09:31
【问题描述】:
我在处理这段代码和插入函数时遇到了问题。我只是想制作一个(非常)简单的计算器,当我使用插入功能时收到错误消息。
非常感谢任何帮助。谢谢!
import tkinter as tk
global square
global multiply
global divide
global minus
global add
global finalnum
square = 0
multiply = 0
divide = 0
minus = 0
add = 0
finalnum = 0
class Calculator(tk.Tk):# class for calculator
def __init__(self):
tk.Tk.__init__(self)
self.wm_title('Calculate!')# changes widow title
self.canvas = tk.Canvas(self, width=150, height=100)
self.grid_rowconfigure(0, weight=1)
self.entry = tk.Entry(self).grid(row=1, column=1, columnspan=4)
self.button = tk.Button(self, width=3, height=1, text='9', command=self.nine).grid(row=3, column=4)
self.button = tk.Button(self, width=3, height=1, text='8', command=self.eight).grid(row=3, column=3)
self.button = tk.Button(self, width=3, height=1, text='7', command=self.seven).grid(row=3, column=2)
self.button = tk.Button(self, width=3, height=1, text='6', command=self.six).grid(row=4, column=4)
self.button = tk.Button(self, width=3, height=1, text='5', command=self.five).grid(row=4, column=3)
self.button = tk.Button(self, width=3, height=1, text='4', command=self.four).grid(row=4, column=2)
self.button = tk.Button(self, width=3, height=1, text='3', command=self.three).grid(row=5, column=4)
self.button = tk.Button(self, width=3, height=1, text='2', command=self.two).grid(row=5, column=3)
self.button = tk.Button(self, width=3, height=1, text='1', command=self.one).grid(row=5, column=2)
self.button = tk.Button(self, width=3, height=1, text='0', command=self.zero).grid(row=6, column=3, ipady =10)
self.button = tk.Button(self, width=3, height=1, text='+', command=self.add).grid(row=9, column=3)
self.button = tk.Button(self, width=3, height=1, text='-', command=self.minus).grid(row=9, column=2)
self.button = tk.Button(self, width=3, height=1, text='÷', command=self.divide).grid(row=10, column=2)
self.button = tk.Button(self, width=3, height=1, text='x', command=self.multiply).grid(row=10, column=3)
self.button = tk.Button(self, width=3, height=1, text='=', command=self.equals).grid(row=11, column=3)
self.button = tk.Button(self, width=3, height=1, text='√', command=self.root).grid(row=9, column=4)
self.button = tk.Button(self, width=3, height=1, text='^', command=self.square).grid(row=10, column=4)
def zero(self):
global number1
number1 = 0
num1 = 1
if num1 == 1:
global num2
num2 = 0
def one(self):
global number1
number1 = 1
num1 = 1
if num1 == 1:
global num2
num2 = 1
def two(self):
global number1
number1 = 2
num1 = 1
if num1 == 1:
global num2
num2 = 2
def three(self):
global number1
number1 = 3
num1 = 1
if num1 == 1:
global num2
num2 = 3
print(num1, num2)
def four(self):
global number1
number1 = 4
num1 = 1
if num1 == 1:
global num2
num2 = 4
def five(self):
global number1
number1 = 5
num1 = 1
if num1 == 1:
global num2
num2 = 5
def six(self):
global number1
number1 = 6
num1 = 1
if num1 == 1:
global num2
num2 = 6
def seven(self):
global number1
number1 = 7
num1 = 1
if num1 == 1:
global num2
num2 = 7
def eight(self):
global number1
number1 = 8
num1 = 1
if num1 == 1:
global num2
num2 = 8
def nine(self):
global number1
number1 = 9
num1 = 1
if num1 == 1:
global num2
num2 = 9
def add(self):
add = 1
def minus(self):
minus = 1
def divide(self):
divide = 1
def multiply(self):
multiply = 1
def square(self):
square = 1
def root(self):
if num1 == 0:
finalnum = number1**.5
else:
error = 1
def equals(self):
if add == 1:
finalnum = number1 + num2
elif minus == 1:
finalnum = number1 - num2
elif divide == 1:
finalnum = number1 / num2
elif multiply == 1:
finalnum = number1 * num2
elif square == 1:
finalnum = number1 ** num2
self.entry.insert(END, finalnum)
calculator = Calculator()
【问题讨论】:
-
没有人会阅读这些乱七八糟的代码来找出你哪里做错了。如果您需要帮助,请将其缩小到一个最小的示例并发布完整的错误回溯。仅作记录:
global声明需要在您 使用 全局变量的函数中,它们在全局范围内不做任何事情 - 您的add、minus、divide等函数不会这样工作。 -
此外,没有必要将所有这些
global语句放在顶部。完全删除它们根本不会影响代码。
标签: python insert tkinter calculator