【问题标题】:Problem in clearing widgets and adding new ones in loop in Python tkinter gui在 Python tkinter gui 中清除小部件并在循环中添加新小部件的问题
【发布时间】:2020-03-28 07:59:27
【问题描述】:

所以这就是全部...

from tkinter import *
from PIL import ImageTk,Image
import tkinter.font as f

root = Tk()
root.title("KBC")
root.configure(background="#8B008B")
root.geometry("1920x1080")
q="not started"
def start():
    head.place_forget()
    button.place_forget()
    global q
    q="started"

path="KK3O.jpg"
k = Image.open(path)
k = k.resize((1400, 800), Image.ANTIALIAS)
img = ImageTk.PhotoImage(k)
label = Label(root, image=img)
label.place(relwidth=1, relheight=1)


if q=="not started":
    #INTRODUCTION
    path = "K.png"
    k1 = Image.open(path)
    k1 = k1.resize((300,335), Image.ANTIALIAS)
    logo = ImageTk.PhotoImage(k1)
    head = Label(root, image=logo, borderwidth=10, relief="sunken")
    head.place(relx=0.5, rely=0.14, anchor='n')
    myfont = f.Font(family='Maiandra GD')
    button = Button(root, text="START", font=myfont, borderwidth=10, bg="#C5B358", command=start)
    button.place(relx=0.5, rely=0.68, relwidth=0.235, anchor='n')
else:
    #FRAME - 1A
    for i in range(1,6):
        label1 = Label(root, text="HEYYYYYYYYYYYYY",bg="red")
        label1.pack()
root.mainloop()

HOME PAGE IMAGE

THIS HAPPENS WHEN I CLICK START

这些是主页的图片,我希望'''label1'''小部件在我按下开始按钮时会说“HEYYYYYYYYYYYYYY”5次,虽然按钮和图像都消失了,但新标签不会出现。如何找到问题所在?

PS:我还是个初学者,这是我在 tkinter 的第三个项目。

【问题讨论】:

标签: python loops tkinter


【解决方案1】:

将 else 中的代码放到你的函数中,所以你的代码应该是这样的:

from tkinter import *
from PIL import ImageTk,Image
import tkinter.font as f

root = Tk()
root.title("KBC")
root.configure(background="#8B008B")
root.geometry("1920x1080")
q="not started"
def start():
    head.place_forget()
    button.place_forget()
    global q
    q="started"
    for i in range(1,6):
        label1 = Label(root, text="HEYYYYYYYYYYYYY",bg="red")
        label1.pack()

path="KK3O.jpg"
k = Image.open(path)
k = k.resize((1400, 800), Image.ANTIALIAS)
img = ImageTk.PhotoImage(k)
label = Label(root, image=img)
label.place(relwidth=1, relheight=1)


if q=="not started":
    #INTRODUCTION
    path = "KK3O.jpg"
    k1 = Image.open(path)
    k1 = k1.resize((300,335), Image.ANTIALIAS)
    logo = ImageTk.PhotoImage(k1)
    head = Label(root, image=logo, borderwidth=10, relief="sunken")
    head.place(relx=0.5, rely=0.14, anchor='n')
    myfont = f.Font(family='Maiandra GD')
    button = Button(root, text="START", font=myfont, borderwidth=10, bg="#C5B358", command=start)
    button.place(relx=0.5, rely=0.68, relwidth=0.235, anchor='n')

root.mainloop()

【讨论】:

  • 非常感谢 Alexey,它现在工作正常,但我希望我可以在函数之外编写 label1,因为我打算添加更多小部件,这可能会使事情变得非常复杂,但谢谢非常感谢您的建议
【解决方案2】:

首先,当您使用未在函数中定义的变量时(例如“start”中的“head”和“button”,您希望将它们作为参数传递或将它们设为全局并使用 global 关键字就像你对“q”所做的那样。

现在,一般来说,在更新小部件时,您希望使用 .update() 方法更新您的 Tk 对象。

【讨论】:

  • 你好汤姆,你能不能更具体地说一下 .update() 方法,因为我不熟悉 tkinter
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 2023-03-18
  • 1970-01-01
  • 2021-04-12
  • 2021-06-08
  • 2019-04-23
相关资源
最近更新 更多