Python Tkinter Withdraw

时间:2014-06-20 12:53:02

标签: python tkinter

这是我的代码:

import tkinter
import tkinter.messagebox as tkmessagebox
from tkinter import *

def Top_Withdraw(top):
    top.withdraw()
    Reaction()

def Reaction():
    top2 = Tk()
    B0 = Label(top2, text = "LOGGED IN", fg = "green", bg = "black", bd = 8)
    B0.pack()
    B1 = tkinter.Button(top2, text = "Character Creator")
    B1.pack(side = LEFT)
    B2 = tkinter.Button(top2, text = "Saved Characters")
    B2.pack(side = RIGHT)


def Reaction2():
    A7 = Label(top, text = "Password saved.")
    A7.pack(side = BOTTOM)

def Character_Creator():
    top3 = Tk()
    C0 = Label(top3, text = "CHARACTER CREATOR", bd = 8)
    C0.pack()



def Login(top):
    A0 = Label(top, text = "WELCOME TO HORSEGARN", fg = "red", bd = 8)
    A0.pack(side = TOP)
    A1 = Label(top, text = "Username")
    A1.pack()
    A2 = Entry(top, bd = 3)
    A2.pack()
    A3 = Label(top, text = "Password")
    A3.pack()
    A4 = Entry(top, show = "*", bd = 3)
    A4.pack()
    A5 = tkinter.Button(top, text = "Log In", command = Top_Withdraw(top))
    A5.pack() 
    var = IntVar()
    A6 = tkinter.Checkbutton(top, text = "Remember password", variable = var, command = Reaction2)
    A6.pack()


top = Tk()
Login(top)

使用此代码我试图最小化定义为" top"当按钮定义为" A5"单击使用" top.withdraw"命令。但是,当我尝试运行程序时,由于某种原因它不会从函数Login(顶部)开始,而是从Reaction()开始。

从逻辑上看,程序不应该从Login(顶部)开始,只有当单击定义为A5的按钮时才转移到Reaction()WHEN?我无法看到为什么它会立即跳到Reaction()。

我试图定义" top"在Login()内,在Reaction()内,以及作为全局变量,但这些都不起作用。我不应该使用提款吗?

1 个答案:

答案 0 :(得分:1)

您的代码中几乎没有其他错误。但是,对应于您所讨论的问题,有两个主要问题。

以下行实际上运行命令,因为您传递了参数。 Tkinter期望命令参数是实例,而不是调用。所以在这里你传递一个电话,所以它运行那个电话。这就是它运行Top_Withdraw方法的原因:

A5 = tkinter.Button(top, text = "Log In", command = Top_Withdraw(top))

要解决此问题,请使用lambda。如果你必须将参数传递给按钮方法,那么你将不得不像这样使用lambda:

A5 = tkinter.Button(top, text = "Log In", command = lambda: Top_Withdraw(top))

此外,您的程序永远不会运行,因为您从未将根窗口置于主循环中。所以一定要在最后一行添加:

top.mainloop()

这将使您的程序启动并运行并使其“消失”#34;当你按照要求点击登录按钮时。但是我肯定会考虑改变一些东西,比如你的进口。你实际上以不同的方式导入tkinter 3次,然后在你的方法中以不同的方式调用它们。您也可以考虑将所有这些放在一个类中。我的2美分。

相关问题