- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我知道这已经涵盖了很多。但是,尽管访问了与更改标签文本有关的每个 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()
请原谅我糟糕的编码。其中大部分是去年类(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/
在 How to make a Tkinter window jump to the front? 中提出的问题之后的一个问题 我希望有一个顶层窗口(我用它来导航我的其他主窗口)总是在前面。但我希望它
有没有办法在 Tkinter 中保持小部件(特别是图像)的堆叠顺序一致?例如,我可能在 Canvas 上的同一位置有两个矩形、两个三角形和一个圆。圆圈移动到最后一次点击鼠标的地方,但我总是希望它被绘制
这是一个简单的 GUI 程序,用于创建 5x16 按钮矩阵。 from tkinter import * root = Tk() button = [[0 for x in range(16)] fo
有一个错误:“AttributeError: module 'tkinter' has no attribute 'messagebox'” 即使 import tkinter 一开始就已经给出了,为
我知道 menu.tk_popup() 可用于在特定坐标处打开上下文菜单,但也不知道如何从中打开子菜单,如果这有意义的话。这是我编写的代码: import tkinter as tk root = t
我正在尝试在禁用自动换行和水平滚动条的文本窗口中书写,如下所示: root = Toplevel() root.geometry("%dx%d+0+0" % (350,400)) af=Frame(r
已经将文本变量分配给小部件后,如何将其删除? widget.config(textvariable=None)只是不工作。在谷歌或这里找不到任何东西。 最佳答案 将您的变量分配给一个空字符串以实现此目
Jython 支持 Tkinter 吗?如果我用 Python 编写一个程序并放一个 使用 Tkinter 的 GUI 前端,做同样的事情有多难 Jython 中的程序?或者对于 Jython GUI
因此,我尝试创建一个 tkinter 窗口,显示当前时间和日期以及自定义短语。不过,我遇到的问题是,我似乎无法在第二天刷新日期。 我可以传递截至运行代码时的当前日期,但之后它变为静态。 这是我目前的程
我的理解是在初始化 __init__ 中的所有框架和小部件之后方法,tkinter 窗口会调整大小以适合所有这些组件。 我想将窗口的初始化大小设置为其最小大小。我希望能够最大化并放大窗口,但我从不希望
此代码仅水平居中,如何使进度条也垂直居中? import Tkinter import ttk root = Tkinter.Tk() root.geometry("=500x500") root.p
使用 Python 2.7 和 Tkinter 模块,我创建了一个菜单按钮并为其分配了一个菜单。现在每次我在特定位置发布菜单时,菜单的宽度都会根据字符数自动设置。有没有办法在菜单小部件中设置静态宽度?
我想将我的 tkinter 应用程序的主题更改为 clam。 代码是什么,我把它放在哪里?我试过了: from tkinter import * from tkinter.ttk import * s
我有以下代码: from Tkinter import * from urllib import urlretrieve import webbrowser import ttk def get_la
我知道,如果我将滚动条控制的框架绑定(bind)到函数 ( onFrameConfigure ),您可以获得滚动条位置,如下所示:self.calendar_frame.bind("", self.o
许多网站都说菜单小部件有一个选项“字体”,但我一直无法设置它。系统是在 Windows 8.1 中运行的 Python 3.5。脚本开始: 从 tkinter 导入 * 根 = Tk() root.g
我正在阅读本教程,它帮助我同时学习 tkinter 和 wxWidgets,但我想深入挖掘,所以想知道哪个 GUI 工具更适合深入学习,为什么? 最佳答案 不可能说哪个“更好”。两者均可用于最常见的用
看书学python,tkinter.END用在一段代码里不用解释 import tkinter def count(text, out_data): """ Update out_data w
我正在尝试使用 Python 2.7 将 Tkinter 导入到我的项目中,但我收到了错误: ImportError: No module named tkinter 在有人说之前,我已经尝试了“Tk
当我回答 Tkinter 问题时,我通常会尝试自己运行代码,但有时我会收到此错误: Traceback (most recent call last): File "C:\Python27\pyg
我是一名优秀的程序员,十分优秀!