Issue with tkinter __init__



我正在写下面的代码,但是当运行时,它抛出了一个关于main_window和关于tkinter的Widget.__init__()的错误:

from tkinter import *
import random
class window_one:
def init(self):
Button(main_window,text="leave, do not return",command=self.quit).grid()
number1 = Button(main_window="ένα/1",font="Gothic 26",bg="red",width=3,command=self.one)
number2 = Button(main_window="δύο/2",font="Gothic 26",bg="red",width=3,command=self.two)
number3 = Button(main_window="τρία/3",font="Gothic 26",bg="red",width=3,command=self.three)
number4 = Button(main_window="τέσσερα/4",font="Gothic 26",bg="red",width=3,command=self.four)
number5 = Button(main_window="πέντε/5",font="Gothic 26",bg="red",width=3,command=self.five)
number6 = Button(main_window="έξι/6",font="Gothic 26",bg="red",width=3,command=self.six)
number7 = Button(main_window="εφτά/7",font="Gothic 26",bg="red",width=3,command=self.seven)
number8 = Button(main_window="οχτώ/8",font="Gothic 26",bg="red",width=3,command=self.eight)
number9 = Button(main_window="εννιά/9",font="Gothic 26",bg="red",width=3,command=self.nine)
canvas.create_window(45,150,window=number1)
canvas.create_window(130,150,window=number2)
canvas.create_window(215,150,window=number3)
canvas.create_window(45,300,window=number4)
canvas.create_window(130,300,window=number5)
canvas.create_window(215,300,window=number6)
canvas.create_window(45,450,window=number7)
canvas.create_window(130,450,window=number8)
canvas.create_window(215,450,window=number9)
def quit(self):
main_window.destroy()
def one(self):
print("You pressed one")
def two(self):
print("You pressed two")
def three(self):
print("You pressed three")
def four(self):
print("You pressed four")
def five(self):
print("You pressed five")
def six(self):
print("You pressed six")
def seven(self):
print("You pressed seven")
def eight(self):
print("You pressed eight")
def nine(self):
print("You pressed nine")
def ten(self):
print("You pressed ten")
main_window = Tk()
canvas = Canvas(main_window, width=600, height=300, bg='grey')
canvas.grid()
window_one()

这是我运行它时得到的结果:

Traceback (most recent call last):
File "C:Users-----------OneDrive - -------- -------DocumentsGreek calculator.py", line 79, in <module>
window_one()
File "C:Users-----------OneDrive - ------DocumentsGreek calculator.py", line 6, in __init__
number1 = Button(main_window="ένα/1",font="Gothic 26",bg="red",width=3,command=self.one)
File "C:Program FilesPython38libtkinter__init__.py", line 2645, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:Program FilesPython38libtkinter__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: unknown option "-main_window"

我检查了tkinter的__init__文件,没有发现任何问题,它运行正常。

你的错误在你发布的代码的第一行(请修复它;它看起来不对)。

您的代码包括:Button(main_window="ένα/1"Button类没有选项main_window,因此会引发错误。

你确实定义了一个main_window实例,所以你的意思是:Button(main_window,text="ένα/1", font="Gothic 26",bg="red",width=3,command=self.one)呢?这样你想要的文本也是按钮上的文本。

您得到错误,因为main_window不是创建Button的有效选项。很明显,你的意思是main_window是第一个参数,第二个是text="ένα/1",等等。

你的代码中还有其他一些错误,最明显的是类的初始化方法应该命名为__init__()而不是init()。我已经修复了这个和其他一些事情,以便能够运行你的问题的代码。我还简化了部分代码,并重新格式化了代码,使其更易于阅读。

我强烈建议你阅读并尝试遵循PEP 8 - Python代码风格指南。

from tkinter import *

class window_one:
def __init__(self):
Button(main_window, text="leave, do not return", command=self.quit).grid()
BTN_FNT = ('gothic', '26')
BTN_WTH = 11
BTN_BGR = 'red'
number1 = Button(main_window, text="ένα/1", font=BTN_FNT, bg=BTN_BGR,
width=BTN_WTH, command=self.one)
number2 = Button(main_window, text="δύο/2", font=BTN_FNT, bg=BTN_BGR,
width=BTN_WTH, command=self.two)
number3 = Button(main_window, text="τρία/3", font=BTN_FNT, bg=BTN_BGR,
width=BTN_WTH, command=self.three)
number4 = Button(main_window, text="τέσσερα/4", font=BTN_FNT, bg=BTN_BGR,
width=BTN_WTH, command=self.four)
number5 = Button(main_window, text="πέντε/5", font=BTN_FNT, bg=BTN_BGR,
width=BTN_WTH, command=self.five)
number6 = Button(main_window, text="έξι/6", font=BTN_FNT, bg=BTN_BGR,
width=BTN_WTH, command=self.six)
number7 = Button(main_window, text="εφτά/7", font=BTN_FNT, bg=BTN_BGR,
width=BTN_WTH, command=self.seven)
number8 = Button(main_window, text="οχτώ/8", font=BTN_FNT, bg=BTN_BGR,
width=BTN_WTH, command=self.eight)
number9 = Button(main_window, text="εννιά/9", font=BTN_FNT, bg=BTN_BGR,
width=BTN_WTH, command=self.nine)
btns = [[number1, number2, number3],
[number4, number5, number6],
[number7, number8, number9]]
for i in range(3):
y = i*75 + 70
for j in range(3):
x = j*200 + 125
canvas.create_window(x, y, window=btns[i][j])
def quit(self):
main_window.destroy()
def one(self):
print("You pressed one")
def two(self):
print("You pressed two")
def three(self):
print("You pressed three")
def four(self):
print("You pressed four")
def five(self):
print("You pressed five")
def six(self):
print("You pressed six")
def seven(self):
print("You pressed seven")
def eight(self):
print("You pressed eight")
def nine(self):
print("You pressed nine")
def ten(self):
print("You pressed ten")
main_window = Tk()
canvas = Canvas(main_window, width=650, height=300, bg='grey')
canvas.grid()
window_one()
main_window.mainloop()  # ADDED.

最新更新