按钮是^
。
应该在红色突出显示的地方,但是它不会去那里-不管我告诉它去哪里,它都会一直去那里(除非我完全移除了self.button.grid(row=x,column=y)
。
这是我的(整个)代码:
import tkinter as tk
import math
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.grid()
self.create_widgets()
self.a = ''
self.math1 = 0
self.math2 = 0
self.total = 0
self.operation = '+'
self.show_answer()
self.method = ''
def create_widgets(self):
self.quit_button = tk.Button(self, text='Q', fg='red', command=self.master.destroy)
self.quit_button.grid(row=5, column=1)
self.c_button = tk.Button(self)
self.c_button['text'] = 'C'
self.c_button.command = self.clear_all
self.c_button.grid(row=7, column=1, columnspan=4)
self.c_button.config(width=9, height=1)
self.one_button = tk.Button(self)
self.one_button['text'] = 1
self.one_button['command'] = self.add_1_to_str
self.one_button.grid(row=2, column=1)
self.two_button = tk.Button(self)
self.two_button['text'] = 2
self.two_button['command'] = self.add_2_to_str
self.two_button.grid(row=2, column=2)
self.three_button = tk.Button(self)
self.three_button['text'] = 3
self.three_button['command'] = self.add_3_to_str
self.three_button.grid(row=2, column=3)
self.four_button = tk.Button(self)
self.four_button['text'] = 4
self.four_button['command'] = self.add_4_to_str
self.four_button.grid(row=3, column=1)
self.five_button = tk.Button(self)
self.five_button['text'] = 5
self.five_button['command'] = self.add_5_to_str
self.five_button.grid(row=3, column=2)
self.six_button = tk.Button(self)
self.six_button['text'] = 6
self.six_button['command'] = self.add_6_to_str
self.six_button.grid(row=3, column=3)
self.seven_button = tk.Button(self)
self.seven_button['text'] = 7
self.seven_button['command'] = self.add_7_to_str
self.seven_button.grid(row=4, column=1)
self.eight_button = tk.Button(self)
self.eight_button['text'] = 8
self.eight_button['command'] = self.add_8_to_str
self.eight_button.grid(row=4, column=2)
self.nine_button = tk.Button(self)
self.nine_button['text'] = 9
self.nine_button['command'] = self.add_9_to_str
self.nine_button.grid(row=4, column=3)
self.zero_button = tk.Button(self)
self.zero_button['text'] = 0
self.zero_button['command'] = self.add_0_to_str
self.zero_button.grid(row=5, column=2)
self.equal_button = tk.Button(self)
self.equal_button['text'] = '='
self.equal_button['command'] = self.equal
self.equal_button.grid(row=5, column=3)
self.plus_button = tk.Button(self)
self.plus_button['text'] = '+'
self.plus_button['command'] = self.plus
self.plus_button.grid(row=2, column=4)
self.subtract_button = tk.Button(self)
self.subtract_button['text'] = '-'
self.subtract_button['command'] = self.subtract
self.subtract_button.grid(row=3, column=4)
self.subtract_button.config(width=1, height=1)
self.multiply_button = tk.Button(self)
self.multiply_button['text'] = 'X'
self.multiply_button['command'] = self.multiply
self.multiply_button.grid(row=4, column=4)
self.divide_button = tk.Button(self)
self.divide_button['text'] = '/'
self.divide_button['command'] = self.multiply
self.divide_button.grid(row=5, column=4)
self.divide_button.config(width=1, height=1)
self.square_button = tk.Button(self)
self.square_button['text'] = '²'
self.square_button['command'] = self.square
self.square_button.grid(row=6, column=1)
self.square_button.config(width=1, height=1)
self.square_root_button = tk.Button(self)
self.square_root_button['text'] = '²√'
self.square_root_button['command'] = self.square_root
self.square_root_button.grid(row=6, column=2)
self.square_root_button.config(width=1, height=1)
self.power_button = tk.Button()
self.power_button['text'] = '^'
self.power_button['command'] = self.power
self.power_button.grid(row=6, column=3)
def power(self):
self.operation = '^'
self.a = int(self.a)
self.math1 = self.a
self.a = str(self.a)
self.a = ''
def square_root(self):
self.operation = '2root'
self.a = int(self.a)
self.math1 = self.a
self.a = 0
self.equal()
self.show_answer()
def square(self):
self.operation = '^2'
self.a = int(self.a)
self.math1 = self.a
self.a = 0
def show_answer(self):
self.answer_box = tk.Button(self)
self.answer_box['text'] = self.total
self.answer_box.grid(row=1, column=1, columnspan=4)
self.answer_box.config(width=9, height=1)
def plus(self):
self.operation = '+'
self.a = int(self.a)
self.math1 = self.a
self.a = str(self.a)
self.a = ''
def subtract(self):
self.operation = '-'
self.a = int(self.a)
self.math1 = self.a
self.a = str(self.a)
self.a = ''
def multiply(self):
self.operation = '*'
self.a = int(self.a)
self.math1 = self.a
self.a = str(self.a)
self.a = ''
def divide(self):
self.operation = '/'
self.a = int(self.a)
self.math1 = self.a
self.a = str(self.a)
self.a = ''
def equal(self):
self.a = int(self.a)
self.math2 = self.a
self.math1 = int(self.math1)
self.math2 = int(self.math2)
if self.operation == '+':
self.total = self.math1 + self.math2
elif self.operation == '-':
self.total = self.math1 - self.math2
elif self.operation == '*':
self.total = self.math1 * self.math2
elif self.operation == '/':
self.total = self.math1 / self.math2
elif self.operation == '^2':
self.total = self.math1 * self.math1
elif self.operation == '2root':
self.total = math.sqrt(self.math1)
elif self.operation == '^':
self.total = 1
for a in range(0, self.math2):
self.total = self.total * self.math1
self.math1 = str(self.math1)
self.math2 = str(self.math2)
self.total = str(self.total)
if self.operation == '^2':
print(self.math1 + 'X' + self.math1 + '=' + self.total)
else:
print(self.math1 + self.operation + self.math2 + '=' + self.total)
self.math1 = 0
self.math2 = 0
self.a = '0'
self.answer_box.destroy()
self.show_answer()
self.total = 0
def add_1_to_str(self):
self.a = self.a + '1'
def add_2_to_str(self):
self.a = self.a + '2'
def add_3_to_str(self):
self.a = self.a + '3'
def add_4_to_str(self):
self.a = self.a + '4'
def add_5_to_str(self):
self.a = self.a + '5'
def add_6_to_str(self):
self.a = self.a + '6'
def add_7_to_str(self):
self.a = self.a + '7'
def add_8_to_str(self):
self.a = self.a + '8'
def add_9_to_str(self):
self.a = self.a + '9'
def add_0_to_str(self):
self.a = self.a + '0'
def clear_all(self):
self.a = '0'
self.math1 = 0
self.math2 = 0
print(self.a)
self.answer_box.destroy()
self.total = 0
self.show_answer()
root = tk.Tk()
app = Application(master=root)
app.mainloop()
是否有办法使其正确进入self.button.grid(row=6, column=3)
?
(编辑)数学效果很好。
答案 0 :(得分:1)
更改
self.power_button = tk.Button()
进入
self.power_button = tk.Button(self)