使用Tkinter动态创建按钮

时间:2016-01-22 06:21:51

标签: python tkinter

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

New.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()

`

1 个答案:

答案 0 :(得分:0)

您正在使用packgrid两个共享共同父级(top)的小部件。您必须只使用其中一个。当您同时使用它们时,您将获得您描述的行为。