如何为我的函数的每次执行更新 Tkinter 中的小部件?

发布于 2025-01-20 13:37:07 字数 991 浏览 0 评论 0原文

从本质上讲,我使用的是使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2025-01-27 13:37:07

第二个用户名的详细信息堆叠在第一个之上,因为第一个用户名的详细信息未从屏幕上删除。因此,标签彼此重叠。

要解决问题,可以使用.destroy()删除以前的用户名标签,如下所示

# Function that takes place when the button is clicked
def bot():
    global ck
    
    # Praw gathers info about the selected user
    username = search_box.get()
    comment_karma = r.redditor(username).comment_karma
    
    try:
        ck.destroy()
    except NameError:
        pass
        
    # 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)

:除首次创建ck时,使用块用于防止错误),

您也可以遵循相同的方法来管理其他标签。

但是,由于您为每个用户名创建新标签,这种方法并不是记忆效率的。一种更好的方法是事先创建标签,并使用以下方式更改每个用户名的值:

<label-widget>.configure(text = <text>)

以下工作代码中给出了内存效率的方法。


工作代码:

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)

#Comment Karma
ck = Label(root)
ck.grid(column=1, row=1)

# Function that takes place when the button is clicked
def bot():
    # Praw gathers info about the selected user
    username = search_box.get()
    comment_karma = r.redditor(username).comment_karma
    
    #Changing the value of comment karma
    ck.configure(text = comment_karma)

search = Button(
    root,
    text="search",
    command=bot,
)
search.grid(column=0, row=1, sticky=E)

root.mainloop()

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:

# Function that takes place when the button is clicked
def bot():
    global ck
    
    # Praw gathers info about the selected user
    username = search_box.get()
    comment_karma = r.redditor(username).comment_karma
    
    try:
        ck.destroy()
    except NameError:
        pass
        
    # 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)

(Notice that ck is a global variable and the try-except block is used to prevent an error when ck 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:

<label-widget>.configure(text = <text>)

The memory-efficient approach is given in the below working code.


Working Code:

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)

#Comment Karma
ck = Label(root)
ck.grid(column=1, row=1)

# Function that takes place when the button is clicked
def bot():
    # Praw gathers info about the selected user
    username = search_box.get()
    comment_karma = r.redditor(username).comment_karma
    
    #Changing the value of comment karma
    ck.configure(text = comment_karma)

search = Button(
    root,
    text="search",
    command=bot,
)
search.grid(column=0, row=1, sticky=E)

root.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文