tkinter - Create new frame with new contents python -


how can create new frame , pass values through pyhton?

code:

 import tkinter tk  import tkmessagebox  import json  import requests  urllib import urlopen  import os.path  import threading 

class myapp(tk.frame):

def callfunc(self,title = "", author = "", body = ""):     tk.frame.__init__(self, top)     self.canvas = tk.canvas(top, borderwidth=0, background="#ffffff")     self.frame = tk.frame(self.canvas, background="#ffffff")     self.vsb = tk.scrollbar(top, orient="vertical", command=self.canvas.yview)     self.canvas.configure(yscrollcommand=self.vsb.set)      self.vsb.pack(side="right", fill="y")     self.canvas.pack(side="left", fill="both", expand=true)     self.canvas.create_window((4,4), window=self.frame, anchor="nw",                                tags="self.frame")      self.frame.bind("<configure>", self.onframeconfigure)     print title     data_json = ""     l1 = tk.label(self, text="title")     l1.pack()     e1 = tk.entry(self, bd =5)     e1.pack()     #e1.insert(0,title)      l2 = tk.label(self, text="author")     l2.pack( )     e2 = tk.entry(self, bd =5)     e2.pack()     #e2.insert(0,author)      l3 = tk.label(self, text="body")     l3.pack( )     e3 = tk.entry(self, bd =5)     e3.pack()     #e3.insert(0,body)     if (title !=""):         data = {"author": e2.get(),         "body" : e3.get(),         "title" : e1.get()}         data_json = json.dumps(data)     self.submitbutton = tk.button(self,text="submit", fg="white", bg="#0094ff",                                  font=("grobold", 10), command = lambda: self.submitfunc(data_json))     self.submitbutton.pack()     #submitbutton.pack()  def submitfunc(self,data_json):     headers = {'content-type': 'application/json', 'accept': 'text/plain'}     url = 'http://localhost/spritle/api.php?action=insert_list&data_json='     check = self.connected_to_internet(url)     if(check):         r = requests.post(url+data_json ,headers=headers )         print r.status_code         # if (r.status_code == 200):         #     tkmessagebox.showinfo("result","success")         # else:         #     if(os.path.isfile("offline_post.json")):         #         open('offline_post.json','a') f:         #             f.write(data_json+"\n")         #     else:         #         open('offline_post.json', 'a')         #         open('offline_post.json','a') f:         #             f.write(data_json+"\n")  # homebutton = button(text="home", fg="white", bg="#0094ff",  #                               font=("grobold", 10), command = view) # homebutton.pack()  def connected_to_internet(self,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 onframeconfigure(self,event):     self.canvas.configure(scrollregion=self.canvas.bbox("all"))   def __init__(self, parent):             self.home()  def home(self):     tk.frame.__init__(self, top)     self.canvas = tk.canvas(top, borderwidth=0, background="#ffffff")     self.frame = tk.frame(self.canvas, background="#ffffff")     self.vsb = tk.scrollbar(top, orient="vertical", command=self.canvas.yview)     self.canvas.configure(yscrollcommand=self.vsb.set)      self.vsb.pack(side="right", fill="y")     self.canvas.pack(side="left", fill="both", expand=true)     self.canvas.create_window((4,4), window=self.frame, anchor="nw",                                tags="self.frame")      self.frame.bind("<configure>", self.onframeconfigure)     self.row=0     # url = "http://localhost/spritle/api.php?action=get_users";     # r = requests.get(url)     # j = r.json()     e1 = tk.label(self.frame, text="title")     e1.grid(row=self.row, column=0)     e1 = tk.label(self.frame, text="author")     e1.grid(row=self.row, column=1)     e1 = tk.label(self.frame, text="body")     e1.grid(row=self.row, column=2)     j = [[0 x in range(5)] x in range(5)]      # j[0]['title'] = "asdg"     # j[0]['author'] = "asdg"     # j[0]['body'] = "asdg"     print j     val in j:         self.row += 1         t1 = tk.label(self.frame, text=val)         t1.grid(row=self.row, column=0)         a1 = tk.label(self.frame, text=val)         a1.grid(row=self.row, column=1)         b1 = tk.label(self.frame, text=val)         b1.grid(row=self.row, column=2)         editbutton = tk.button(self.frame, text="edit", fg="white", bg="#0094ff",                                   font=("grobold", 10),command = lambda title=val,author=val,body=val: self.callfunc(title,author,body))         editbutton.grid(row=self.row,column=3)     #     editbutton.pack()     newbutton = tk.button(self.frame, text="new post", fg="white", bg="#0094ff",                                  font=("grobold", 10),command =  lambda:self.callfunc)     newbutton.grid(row = self.row + 1, column = 2)   top = tk.tk()  top.title("app")  sizex = 400  sizey = 300  posx  = 50  posy  = 50  top.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))  app = myapp(top)  top.mainloop() 

i new gui programmer. there way it? or can overwrite old frame itself? tried same frame contents not overriding display every thing ....

i think may confusing frames toplevels (windows) frame generic container when not given parent generate window of it's own create new windows need make toplevel widget:

class example_app(tk.frame):     def __init__(self,master=none):         tk.frame.__init__(self,master)         self.master.title("still master")          self.windows = []         self.new_button = tk.button(self,text="make new window",command=self.new_window)         self.new_button.grid()         self.del_button = tk.button(self,text="delete window",command=self.del_window)         self.del_button.grid()      def new_window(self,event=none):         new = tk.toplevel(self)         new.title("window %d"%len(self.windows))         tmp_label = tk.label(new,text="here label")         tmp_label.grid()          #it temporary because reference lost when function finishes, preferable have separate class         self.windows.append(new)      def del_window(self,event=none):         self.windows.pop().destroy()  app = example_app() #note don't have explicitly specify root app.grid() #still have grid frame master  app.mainloop() 

if new window needs have stuff on (which of course does!) may consider making class popup:

class popup(tk.toplevel):     def __init__(self,tk_master,title=none,author=none,body=none):         #most of code callfunc() method, changed master widgets         tk.toplevel.__init__(self,tk_master)         if title:             self.title(title)         self.canvas = tk.canvas(self, borderwidth=0, background="#ffffff")         self.frame = tk.frame(self.canvas, background="#ffffff")         ...      def onframeconfigure(self,event):         self.canvas.configure(scrollregion=self.canvas.bbox("all"))    class myapp(tk.frame):     def callfunc(self,title = "", author = "", body = ""):         popup = popup(self.parent,title,author,body)      ...      def __init__(self, parent):         self.parent  = parent         self.home()      def home(self):         tk.frame.__init__(self, self.parent) #wouldn't make more sense in __init__?         ... 

Comments