2

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?

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

`

4
  • What happens when you remove this button ? Is it working again ? Commented Jan 22, 2016 at 6:38
  • yes it working...if i remove newbutton and editbutton Commented Jan 22, 2016 at 6:39
  • is it working when you remove only newbutton or only editbutton ? Commented Jan 22, 2016 at 6:42
  • it is working then i remove both newbutton.pack() and editbutton.pack()... Commented Jan 22, 2016 at 6:46

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.