Python 第二页没有显示?
这是我的代码:Python 第二页没有显示?,python,tkinter,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
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”时,它会显示一个文本,在我的代码中显示“这是我的文本”。但当我运行它时,我点击“下一步”没有任何东西显示出来,我检查了又检查,但似乎没有任何工作。任何帮助都将不胜感激
# 我不是专家,但我会试试看 首先,不需要导入系统。不建议使用tkinter import*中的
从tkinter模块导入所有对象。您应该使用import tkinter
或import tkinter as tk
,以避免未预料到的后果
您有两个同名的函数<代码>下一个屏幕(名称)
不应出现
与其使用widgets=[mLabel1,button]
隐藏小部件,不如将它们放在一个框架中,以便使用winfo_children()
查找所有子部件
当您创建按钮和标签时,您应该放置父窗口小部件名称
。比如说,
import tkinter as tk
root = tk.Tk()
mylabel = tk.Label(root,text='this is a label')
mylabel.pack()
root.mainloop()
在第一个next\u屏幕(名称)
功能中,使用grid方法
显示按钮。您不应该混合使用网格方法
和放置方法
这是我想到的
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()
希望有帮助 在下一个_screen函数中,我删除了斜边的()?你有两个!!!程序只考虑第二种情况。请更正代码并显示您得到的输出。因为这段代码无法工作。re“您不应该使用网格方法仅显示1项。”:为什么这么说?对一个项目使用网格没有什么错,尽管在这种情况下,
pack
通常更方便。否则,很好地指出所有缺陷。@BryanOakley我已经删除了你指出的那条线。我一直认为网格管理器适用于类似表格的结构。当您只有一个项目时,使用行和列
似乎不符合逻辑(至少对我来说)。但我几个月前才开始学习Tkinter,所以我可能错了。