如何为我的函数的每次执行更新 Tkinter 中的小部件?
从本质上讲,我使用的是使用TKINTER中的第一个Go中的Praw的Reddit机器人。我的代码的最小工作版本如下:
from tkinter import *
import praw
# Praw bot
r = praw.Reddit(
user_agent="user_agent",
client_id="client_id",
client_secret="client_secret",
username="username",
password="password",
)
root = Tk()
search_box = Entry(root)
search_box.grid(column=0, row=1, sticky=W)
# Function that takes place when the button is clicked
def bot():
# Praw gathers info about the selected user
username = Entry.get(search_box)
comment_karma = r.redditor(username).comment_karma
# Labels are created for the collected data
ck = Label(
root,
text=comment_karma
)
# Labels are made visible to the user
ck.grid(column=1, row=1)
search = Button(
root,
text="search",
command=bot,
)
search.grid(column=0, row=1, sticky=E)
root.mainloop()
它的作用是收集有关输入任何reddit用户名的信息,并在窗口中创建并添加了带有信息的标签。但是,如果我比输入另一个用户名并重复该功能,则它将其堆放在上一个小部件的顶部。我该如何更改?
I essentially have a reddit bot using PRAW integrated into my first go at using Tkinter. A minimal working version of my code is as follows:
from tkinter import *
import praw
# Praw bot
r = praw.Reddit(
user_agent="user_agent",
client_id="client_id",
client_secret="client_secret",
username="username",
password="password",
)
root = Tk()
search_box = Entry(root)
search_box.grid(column=0, row=1, sticky=W)
# Function that takes place when the button is clicked
def bot():
# Praw gathers info about the selected user
username = Entry.get(search_box)
comment_karma = r.redditor(username).comment_karma
# Labels are created for the collected data
ck = Label(
root,
text=comment_karma
)
# Labels are made visible to the user
ck.grid(column=1, row=1)
search = Button(
root,
text="search",
command=bot,
)
search.grid(column=0, row=1, sticky=E)
root.mainloop()
What it does is gather information about whatever reddit username was inputted and creates and adds Labels with the info into the window. However, if I than input another username and repeat the function, it stacks on top of the previous widget. How can I change this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个用户名的详细信息堆叠在第一个之上,因为第一个用户名的详细信息未从屏幕上删除。因此,标签彼此重叠。
要解决问题,可以使用
.destroy()
删除以前的用户名标签,如下所示:除首次创建
ck
时,使用块用于防止错误),您也可以遵循相同的方法来管理其他标签。
但是,由于您为每个用户名创建新标签,这种方法并不是记忆效率的。一种更好的方法是事先创建标签,并使用以下方式更改每个用户名的值:
以下工作代码中给出了内存效率的方法。
工作代码:
The details of the second username are stacked on top of the first because the first username details are not removed from the screen. Therefore, the labels overlap with each other.
To solve the problem, the previous username labels can be removed using
.destroy()
as shown below:(Notice that
ck
is a global variable and thetry-except
block is used to prevent an error whenck
is created for the first time)You can follow the same approach to manage the other labels as well.
But this approach is not memory-efficient as you are creating new labels for every username. A better approach would be to create the labels beforehand and just change their values for every username using:
The memory-efficient approach is given in the below working code.
Working Code: