python - 修改 Tkinter 标签

标签 python tkinter label

我知道这已经被讨论了很多。然而,尽管访问了每个与更改标签文本有关的 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。因此,每次单击按钮时,都会创建一个新的电影标签,它会覆盖之前生成的标签。

您想要的是一个标签,每次单击该按钮时,其文本都会相应更改。因此,您需要在 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/

相关文章:

python 32位 float 转换

python - 将数据帧写入 pandas 中的 csv 时,选项卡未显示

python - Python中类属性、实例属性和实例方法的区别

python - 未绑定(bind)本地错误 : local variable 'output' referenced before assignment

IOS:为标签组织动画

python - KD/Qtree 实现

python - __init__() 中的类型错误,意外的参数 python

python - 如何在函数之间传递变量?

CSS更改单选按钮标签

android - 如何仅将自定义渲染器添加到某些特定布局?