第2页没出现?

时间:2013-08-11 16:12:52

标签: python tkinter

这是我的代码:

import sys
from tkinter import *


#first new screen
def next_screen(names):
    for widget in names:
        widget.place_forget()
        buttonhyp = Button (text = "button1",fg = "blue",command = hypoténusegetdef())
        buttonhyp.grid (row = 1,column = 2)

def forget_page1():
    widgets = [mLabel1, button]
    next_screen(widgets)

################################################################################

def hypténusegetdef ():
    widgets1 = [buttonhyp]
    nextscreen1(widgets1)

def next_screen(names):
    for widget in names:
        widget.place_forget() 
        hyplabel1 = Label (text = "This is my text")








#first page things
mGui = Tk ()

mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = Label (text = "Welcome to MyMathDictionary. Press Next to continue.",
                 fg = "blue",bg = "white")
mLabel1.place (x= 150,y = 200)

button = Button (text = "Next", command = forget_page1 )
button.place(x = 275,y = 230)




mGui.mainloop()

我要做的是打开程序并让用户点击“下一步”,然后显示另一个名为“button1”的按钮,当用户点击“button1”时,它会显示一个在我的代码中称为“这是我的文本”的文本。但是当我运行它时,我点击“下一步”,没有任何显示我检查并重新检查但似乎没有任何工作。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我不是专家,但我会尝试一下。

首先,import sys不是必需的。并且不建议使用from tkinter import*从tkinter模块导入所有对象。您应该使用import tkinterimport tkinter as tk来避免意外后果。

您有2个具有相同名称的功能。 next_screen(names)这不应该发生。

不应使用widgets = [mLabel1, button]来隐藏小部件,而应将它们放在一个框架中,以便您可以使用winfo_children()查找所有子窗口小部件。

您应该在创建按钮和标签时​​放置parent widget name。例如,

import tkinter as tk
root = tk.Tk()
mylabel = tk.Label(root,text='this is a label')
mylabel.pack()
root.mainloop()

在您的第一个next_screen(names)功能中,您使用grid method来显示按钮。您不应混用grid methodplace method

这是我想出的东西

import tkinter as tk

def Screen_1():
    Clear(myframe) 
    mybutton2= tk.Button(myframe,text = "Button1", command = Screen_2)
    mybutton2.pack()

def Screen_2():
    Clear(myframe)
    mylabel2= tk.Label(myframe,text = "This is my text",fg = "blue",bg = "white")
    mylabel2.pack(side='top')

def Clear(parent):
    for widget in parent.winfo_children():
        widget.pack_forget()

root =tk.Tk()

myframe=tk.Frame(root)
myframe.pack()

mylabel1= tk.Label(myframe,text = "Welcome to MyMathDictionary. Press Next to continue.",fg = "blue",bg = "white")
mylabel1.pack(side='top')

mybutton1= tk.Button(myframe,text = "Next", command = Screen_1)
mybutton1.pack(side='bottom')

root.mainloop()
希望它有所帮助!

相关问题