我正在使用tkinter并尝试创建一个框架库,而不是让我的程序每次都打开新窗口。我已经开始创建一个欢迎页面,我正在尝试显示我创建的仅为它提供此错误消息。 “ValueError:字典更新序列元素#0的长度为1;需要2” 这是我的代码:
#!/usr/bin/python
from tkinter import *
import tkinter as tk
Large_Font = ("Verdana", 18)
class ATM(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side = "top", fill ="both", expand =True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for i in (WelcomePage, Checking):
frame = i(container, self)
self.frames[i] = frame
frame.grid(row= 0, column = 0, sticky= "nsew")
self.show_frame(WelcomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class WelcomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, "Welcome to the ATM Simulator", font = Large_Font)
label.pack(pady=100, padx=100)
checkButton = Button(self, text = "Checking Account",
command = lambda: controller.show_frame(Checking))
checkButton.pack()
class Checking(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, controller)
self.controller = controller
label = tk.Label(self, "Welcome to the ATM Simulator", font = Large_Font)
label.pack(pady=100, padx=100)
homeButton = Button(self, text = "Back to Home Page",
command = lambda: controller.show_frame(WelcomePage))
homeButton.pack()
app = ATM()
app.mainloop()
出现错误消息,因为我说明了
frame = i(container, self)
但是当我创建类我声明
class WelcomePage(tk.Frame):
我的WelcomePage类中的字典元素只有1个参数,但我需要两个。我尝试将self
作为第二个参数,但这不起作用。这在Python 3.4中有效,但现在我使用的是Python 3.5,它给了我这个错误。我该如何解决这个问题?
答案 0 :(得分:2)
class Checking(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, controller)
我不认为Frame
的初始化程序可以接受那么多参数,除非controller
是字典。尝试:
class Checking(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
您还应该使用text
命名参数来指定标签的文本。
label = tk.Label(self, text="Welcome to the ATM Simulator", font = Large_Font)