使用 Tkinter 动态创建按钮

编程入门 行业动态 更新时间:2024-10-24 06:29:53
本文介绍了使用 Tkinter 动态创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 Python 创建 GUI.它一直正常工作,直到我在 __init__ 的 for 循环中包含一个按钮.我在命令提示符下编译时没有收到任何错误.GUI 未打开.导致此错误的原因是什么?

I am creating GUI using Python. It was working correctly until I included a button in for loop at __init__. I am not getting any error when I compile in command prompt. GUI is not opening. What's causing this error?

新.py:

class myapp:

def callfunc(title = "", author = "", body = ""):
    L1 = Label(top, text="Title")
    L1.pack( side = TOP)
    E1 = Entry(top, bd =5)
    E1.pack(side = TOP)
    E1.insert(0,title)

    L2 = Label(top, text="Author")
    L2.pack( side = TOP)
    E2 = Entry(top, bd =5)
    E2.pack(side = TOP)
    E2.insert(0,author)

    L3 = Label(top, text="Body")
    L3.pack( side = TOP)
    E3 = Entry(top, bd =5)
    E3.pack(side = TOP)
    E3.insert(0,body)

    data = {"author": E2.get(),
    "body" : E3.get(),
    "title" : E1.get()}
    data_json = json.dumps(data)
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    url = 'http://localhost/spritle/api.php?action=insert_list&data_json='
    check = connected_to_internet(url)
    if(check):
        r = requests.post(url+data_json ,headers=headers )
        if (r.status_code == 200):
            tkMessageBox.showinfo("Result","success")
        else:
            if(os.path.isfile("offline_post.json")):
                with open('offline_post.json','a') as f:
                    f.write(data_json+"\n")
            else:
                open('offline_post.json', 'a')
                with open('offline_post.json','a') as f:
                    f.write(data_json+"\n")
    SubmitButton = Button(top,text="Submit", fg="White", bg="#0094FF", 
                                font=("Grobold", 10), command = callfunc)
    SubmitButton.pack()

# homeButton = Button(text="Home", fg="White", bg="#0094FF", 
#                               font=("Grobold", 10), command = view)
# homeButton.pack()

def connected_to_internet(url, timeout=5):
    try:
        _ = requests.get(url, timeout=timeout)
        threading.Timer(10, connected_to_internet(url)).start()
        print "asd"
        return True
    except requests.ConnectionError:
        print("No internet connection available.")
        return False

def __init__(self, parent):
    self.row=0
    url = "http://localhost/spritle/api.php?action=get_users";
    r = requests.get(url)
    j = r.json()
    E1 = Label(top, text="Title")
    E1.grid(row=self.row, column=0)
    E1 = Label(top, text="Author")
    E1.grid(row=self.row, column=1)
    E1 = Label(top, text="Body")
    E1.grid(row=self.row, column=2)
    for val in j:
        self.row += 1
        T1 = Label(top, text=val['title'])
        T1.grid(row=self.row, column=0)
        A1 = Label(top, text=val['author'])
        A1.grid(row=self.row, column=1)
        B1 = Label(top, text=val['body'])
        B1.grid(row=self.row, column=2)
        editButton = Button(top, text="Edit", fg="White", bg="#0094FF", 
                                font=("Grobold", 5), command = lambda: callfunc(val['title'],val['author'],val['body']))
        editButton.pack()
    newButton = Button(top, text="New Post", fg="White", bg="#0094FF", 
                                font=("Grobold", 5), command = lambda: callfunc)
    newButton.pack()

 top = Tk()
top.title("App")
app = myapp(top)
top.mainloop()

`

推荐答案

您正在将 packgrid 与共享公共父级 (top).您只能使用其中之一.当您同时使用两者时,您将获得所描述的行为.

You are using both pack and grid with widgets that share a common parent (top). You must use only one or the other. When you use both, you will get the behavior that you describe.

这篇关于使用 Tkinter 动态创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:34:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   动态   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!