如何使用Tkinter在类中将按钮方法从包更改为网格

时间:2015-12-06 21:01:27

标签: python tkinter python-3.4

我使用Tkinter通过matplotlib和动画显示绘制的点,以实时查看更新。当我使用pack来放置我的标签和按钮时,一切正常,但我想将按钮放在某个位置,所以决定使用网格。但是现在我收到了一个错误。对于Tkinter来说,我是新手。按照一个例子,我在一个类之外成功地使用了网格。但是当我尝试在班级中将元素从包更改为网格时,我收到错误。这是我的代码:

import tkinter as tk
from tkinter import *
from tkinter import ttk

class loginScreen(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Bank App")

        #tk.Tk.iconbitmap(self, default="filename.ico")
        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 F in (StartPage, PageOne, PageTwo, PageThree):

            frame = F(container, self)

            self.frames[F] = frame


            frame.grid(row=0, column=0, sticky="nsew")


        self.show_frame(StartPage)


    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

...
...

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.rowconfigure(0,weight=1)
        self.columnconfigure(0,weight=1)
        self.grid(column=0, row=0, sticky=(N,W,E,S))

        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.grid(pady=10,padx=10)

        button1 = ttk.Button(self, text="Login",
                        command=lambda: controller.show_frame(PageOne))
        button1.grid(self, column=2, row=2, sticky=(S,E))

        button2 = ttk.Button(self, text="Register",
                        command=lambda: controller.show_frame(PageTwo))
        button2.grid(self, column=3, row=2, sticky=(S,E))

        button3 = ttk.Button(self, text="Graph Page",
                        command=lambda: controller.show_frame(PageThree))
        button3.grid(self, column=3, row=3, sticky=(S,E))

...
...

app = loginScreen()
app.mainloop()

以下是返回的错误:

Traceback (most recent call last):
File "C:\Users\Rozelle\Desktop\tkinter_bankAcct.py", line 166, in <module>
app = loginScreen()
 File "C:\Users\Rozelle\Desktop\tkinter_bankAcct.py", line 65, in __init__
frame = F(container, self)
File "C:\Users\Rozelle\Desktop\tkinter_bankAcct.py", line 95, in __init__
button1.grid(self, column=2, row=2, sticky=(S,E))
 File "C:\Python34\lib\tkinter\__init__.py", line 2057, in grid_configure
+ self._options(cnf, kw))_tkinter.TclError: bad option "-cursor": must be-column, -columnspan, -in, - ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky

1 个答案:

答案 0 :(得分:1)

为了说明向mcve移动,这仍然显示错误。

import tkinter as tk

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.grid()

        button1 = tk.Button(self, text="Login")
        button1.grid(self)

root=tk.Tk()
app = StartPage(root, None)

请注意.grid来电的区别?永远不要在packgrid来电中传递父母。删除self,代码可以正常运行。这是一个真正极小但完整且可验证的例子。使用pack代替grid会产生同样的错误。

import tkinter as tk
root = tk.Tk()
b = tk.Button(root)
b.grid(root)