SHARE
TWEET

Python - tkinter - second window with StringVar

furas Jun 20th, 2018 (edited) 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter as tk
  2.  
  3.  
  4. class Main(tk.Tk):
  5.    
  6.     def __init__(self):
  7.         super().__init__()
  8.  
  9.         self.l = tk.Label(self, text='???')
  10.         self.l.pack()
  11.        
  12.         b = tk.Button(self, text='New', command=self.on_click)
  13.         b.pack()
  14.  
  15.         self.mainloop()    
  16.  
  17.     def on_click(self):
  18.         new = Next(self)
  19.         print('close')
  20.  
  21.  
  22. class Next(tk.Toplevel):
  23.    
  24.     def __init__(self, master): # master - to get access to parent window
  25.         super().__init__(master)
  26.        
  27.         self.var = tk.StringVar() # tk.StringVar(self)
  28.        
  29.         e = tk.Entry(self, textvariable=self.var)
  30.         e.pack()
  31.        
  32.         b = tk.Button(self, text='Close', command=self.on_click)
  33.         b.pack()
  34.        
  35.         #self.mainloop() # no need it
  36.        
  37.     def on_click(self):
  38.         print(self.var.get()) # get text from Entry
  39.         self.master.l['text'] = self.var.get() # put in parent window
  40.         self.destroy()
  41.  
  42.  
  43. Main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top