【发布时间】:2016-02-17 03:11:27
【问题描述】:
我正在使用 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,它给了我这个错误。我该如何解决这个问题?
【问题讨论】:
标签: python dictionary tkinter