使用 Tkinter 动态创建按钮 [英] Dynamically create button using Tkinter

查看:43
本文介绍了使用 Tkinter 动态创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆