gpt4 book ai didi

python - 修改 Tkinter 标签

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:26 25 4
gpt4 key购买 nike

我知道这已经涵盖了很多。但是,尽管访问了与更改标签文本有关的每个 Stackoverflow 链接,但我仍无法解决我的问题。

我试过使用 StringVar().configure() 但没有任何运气。

我想做的是,当用户选择所需的类型并单击“显示电影”时,将显示一个字符串,其中包含该类型下可用的电影。

但是,我面临的问题是,尽管使用 .configure() 更新文本,而不是在顶部创建另一个标签,但标签仍然相互重叠。

这是我的应用程序当前正在做什么的一个小演示:标签重叠

enter image description here

from tkinter import *
import tkinter.ttk
import tkinter.messagebox
import datetime

#
# Created by SAXAR on 04/12/2018.
#

timeNow = datetime.datetime.now() # Creating a variable to use the date time library.

screens = ["Screen 1", "Screen 2", "Screen 3", "Screen 4", "Screen 5", "Screen 6"]

movies = {"Horror": ["The Nun", "Dracula Untold", "Feral", "Shin Godzilla", "Black Death"],
"Action": ["Venom", "Robin Hood", "Aquaman", "Artemis Fowl", "The Predator"],
"Drama": ["Creed", "Creed 2", "Outlaw King", "Peppermint", "Sicario: Day of the Soldado"],
"Comedy": ["Step Brothers", "The Hangover", "Horrible Bosses", "The Other Guys", "Let's Be Cops"],
"Sci-Fi": ["The Matrix", "Solaris", "Blade Runner", "Interstellar", "Sunshine"],
"Romance": ["Ghost", "Sliding Doors", "50 Shades of Grey", "Titanic", "La La Land"]}



class Application(Frame):
def __init__(self, master=None, Frame=None):
Frame.__init__(self, master)
super(Application, self).__init__()
self.createWidgets()

def updateHorror(self, event=None):
selectedGenre = self.genreCombo.get()
print(selectedGenre)
return selectedGenre

def createWidgets(self):
# The heading for the application.
Label(
text="___________________________________________________________________________________________________________________________________________").place(
x=0, y=25)
self.headingLabel = Label(text="Cinema Bookings")
self.headingLabel.config(font=("Roboto", 12))
self.headingLabel.place(x=10, y=10)

Label(text="________").place(x=10, y=65)
Label(text="TODAY").place(x=10, y=60)
Label(text="________").place(x=10, y=42)

Label(text="Genre: ").place(x=70, y=60)
self.genreCombo = tkinter.ttk.Combobox(width=15, values=list(movies.keys()), state="readonly")
self.genreCombo.current(0)
self.genreCombo.bind('<<ComboboxSelected>>', self.updateHorror)
self.genreCombo.place(x=110, y=60)

Label(
text="___________________________________________________________________________________________________________________________________________").place(
x=0, y=85)


Button(text="Display Movie(s)", command=self.createLabel).place(x=585, y=265, width=100)

def createLabel(self, event=None):

self.movieLabel = Label(text = "")
self.movieLabel.place(x=60, y=160)
self.movieLabel.configure(text=" | ".join(movies.get(self.updateHorror())))


w = 700
h = 300
x = 0
y = 0

app = Application()
app.master.geometry("%dx%d+%d+%d" % (w, h, x, y))
app.master.title("Cinema Booking")
app.mainloop()

请原谅我糟糕的编码。其中大部分是去年类(class)作业的先前作业。

最佳答案

发生这种情况的原因是您在 createLabel() 方法中创建了 movielabel。因此,每次单击该按钮时,都会创建一个新的 movielabel,它会覆盖之前生成的标签。

你想要的是一个单一的标签,每次点击按钮时,它的文本都会相应地改变。因此,您需要在 createWidgets() 函数中创建标签,并在 createLabel 函数中配置其文本。

这是工作代码。

from tkinter import *
import tkinter.ttk
import tkinter.messagebox
import datetime

timeNow = datetime.datetime.now() # Creating a variable to use the date time library.

screens = ["Screen 1", "Screen 2", "Screen 3", "Screen 4", "Screen 5", "Screen 6"]

movies = {"Horror": ["The Nun", "Dracula Untold", "Feral", "Shin Godzilla", "Black Death"],
"Action": ["Venom", "Robin Hood", "Aquaman", "Artemis Fowl", "The Predator"],
"Drama": ["Creed", "Creed 2", "Outlaw King", "Peppermint", "Sicario: Day of the Soldado"],
"Comedy": ["Step Brothers", "The Hangover", "Horrible Bosses", "The Other Guys", "Let's Be Cops"],
"Sci-Fi": ["The Matrix", "Solaris", "Blade Runner", "Interstellar", "Sunshine"],
"Romance": ["Ghost", "Sliding Doors", "50 Shades of Grey", "Titanic", "La La Land"]}

class Application(Frame):
def __init__(self, master=None, Frame=None):
Frame.__init__(self, master)
super(Application, self).__init__()
self.createWidgets()

def updateHorror(self, event=None):
selectedGenre = self.genreCombo.get()
print(selectedGenre)
return selectedGenre

def createWidgets(self):
# The heading for the application.
Label(
text="___________________________________________________________________________________________________________________________________________").place(
x=0, y=25)
self.headingLabel = Label(text="Cinema Bookings")
self.headingLabel.config(font=("Roboto", 12))
self.headingLabel.place(x=10, y=10)

Label(text="________").place(x=10, y=65)
Label(text="TODAY").place(x=10, y=60)
Label(text="________").place(x=10, y=42)

Label(text="Genre: ").place(x=70, y=60)
self.genreCombo = tkinter.ttk.Combobox(width=15, values=list(movies.keys()), state="readonly")
self.genreCombo.current(0)
self.genreCombo.bind('<<ComboboxSelected>>', self.updateHorror)
self.genreCombo.place(x=110, y=60)

Label(
text="___________________________________________________________________________________________________________________________________________").place(
x=0, y=85)

Button(text="Display Movie(s)", command=self.createLabel).place(x=585, y=265, width=100)
self.movieLabel = Label(text = "")
self.movieLabel.place(x=60, y=160)

def createLabel(self, event=None):
self.movieLabel.configure(text=" | ".join(movies.get(self.updateHorror())))

w = 700
h = 300
x = 0
y = 0

app = Application()
app.master.geometry("%dx%d+%d+%d" % (w, h, x, y))
app.master.title("Cinema Booking")
app.mainloop()

关于python - 修改 Tkinter 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53624755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com