我知道这已经涵盖了很多。但是,尽管访问了每个与更改标签文本有关的Stackoverflow链接,但我仍无法解决我的问题。
我尝试使用StringVar()
和.configure()
时没有任何运气。
我要做的是,当用户选择所需的流派并单击“显示电影”时,将显示一个字符串,其中包含该流派下可用的电影。
但是,我面临的问题是,尽管使用.configure()来更新文本,而不是在顶部创建另一个标签,但是标签仍然彼此重叠。
这是我的应用程序当前正在做的小展示: 标签重叠
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()
不好意思,我的编码不好。大部分是去年课程中的先前工作。
答案 0 :(得分:3)
发生这种情况的原因是您正在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()