Posts: 157
Threads: 47
Joined: Nov 2021
sorry for my bad english,
i have this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from tkinter import *
def openwindow():
newwindow = Toplevel()
newwindow.minsize( 400 , 200 )
newwindow.title( "new window" )
newentry = Entry(newwindow)
newentry.pack()
newentry.insert(END, f "edit here" )
root = Tk()
root.title( "root" )
root.minsize( 400 , 200 )
button = Button(root, text = 'Create new window' )
button[ 'command' ] = openwindow
button.pack()
root.mainloop()
|
The problem with this code is,
that every time you press the button,
it creates a new window,
But I need a unique window,
that even if you force close "newwindow" using [Alt+F4],
the "newwindow" "Entry" will not lose the data when I press the button again,
how to do that?
please help
Posts: 1,145
Threads: 114
Joined: Sep 2019
You could store it in a variable in the main loop and then pass it back to the window
Posts: 157
Threads: 47
Joined: Nov 2021
(Aug-10-2024, 10:57 PM)menator01 Wrote: You could store it in a variable in the main loop and then pass it back to the window i don't get it, can you provide the code?
Posts: 1,145
Threads: 114
Joined: Sep 2019
This is what I came up with. There are probably better ways.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import tkinter as tk
class Data:
var = 'Edit Here'
class Popup:
def __init__( self ):
self .window = tk.Toplevel( None )
self .var = tk.StringVar()
self .entry = tk.Entry( self .window, textvariable = self .var)
self .entry.pack()
self .entry.focus()
class Window:
def __init__( self , parent):
self .parent = parent
self .button = tk.Button(parent, text = 'Open Window' )
self .button.pack()
class Controller:
def __init__( self , data, window, popup):
self .data = data
self .window = window
self .popup = popup
self .window.button.configure(command = self .openwindow)
def openwindow( self ):
field = self .popup()
field.entry.insert( 'end' , self .data.var)
field.var.trace( 'w' , lambda var,index,mode: self .callback(field.var, 0 , 'w' ))
def callback( self , var, index, mode):
self .data.var = var.get()
if __name__ = = '__main__' :
root = tk.Tk()
controller = Controller(Data(), Window(root), Popup)
root.mainloop()
|
Posts: 157
Threads: 47
Joined: Nov 2021
(Aug-11-2024, 12:18 AM)menator01 Wrote: This is what I came up with. There are probably better ways.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import tkinter as tk
class Data:
var = 'Edit Here'
class Popup:
def __init__( self ):
self .window = tk.Toplevel( None )
self .var = tk.StringVar()
self .entry = tk.Entry( self .window, textvariable = self .var)
self .entry.pack()
self .entry.focus()
class Window:
def __init__( self , parent):
self .parent = parent
self .button = tk.Button(parent, text = 'Open Window' )
self .button.pack()
class Controller:
def __init__( self , data, window, popup):
self .data = data
self .window = window
self .popup = popup
self .window.button.configure(command = self .openwindow)
def openwindow( self ):
field = self .popup()
field.entry.insert( 'end' , self .data.var)
field.var.trace( 'w' , lambda var,index,mode: self .callback(field.var, 0 , 'w' ))
def callback( self , var, index, mode):
self .data.var = var.get()
if __name__ = = '__main__' :
root = tk.Tk()
controller = Controller(Data(), Window(root), Popup)
root.mainloop()
|
Thank you for the reply and the code,
your code is work, the data still there,
but it still creates multiple windows when you press the button,
is there a way to check if the windows exist in the first place?
Posts: 1,145
Threads: 114
Joined: Sep 2019
Aug-11-2024, 01:35 AM
(This post was last modified: Aug-11-2024, 04:08 AM by menator01.)
I altered the code a little. I disabled the button if the popup is open. When the popup is closed, the button will enable again.
You could also use withdraw and deiconify if wanted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import tkinter as tk
class Data:
var = 'Edit Here'
class Popup:
def __init__( self ):
self .window = tk.Toplevel( None )
self .window.geometry( '400x200+300+300' )
self .window.configure(pady = 20 , padx = 10 )
self .var = tk.StringVar()
self .entry = tk.Entry( self .window, textvariable = self .var)
self .entry.pack(fill = 'x' )
self .entry.focus()
class Window:
def __init__( self , parent):
self .parent = parent
self .parent.geometry( '200x200' )
self .parent.configure(pady = 20 )
self .button = tk.Button(parent, text = 'Open Window' )
self .button.pack()
class Controller:
def __init__( self , data, window, popup):
self .data = data
self .window = window
self .popup = popup
self .window.button.configure(command = self .openwindow, cursor = 'hand2' )
def openwindow( self ):
self .field = self .popup()
self .field.entry.insert( 'end' , self .data.var)
self .field.var.trace( 'w' , lambda var,index,mode: self .callback( self .field.var))
self .window.button.configure(state = 'disabled' , cursor = 'pirate' )
self .field.window.protocol( 'WM_DELETE_WINDOW' , self .enable)
def callback( self , var, index = None , mode = None ):
self .data.var = var.get()
def enable( self ):
self .field.window.destroy()
self .window.button.configure(state = 'normal' , cursor = 'hand2' )
if __name__ = = '__main__' :
root = tk.Tk()
controller = Controller(Data(), Window(root), Popup)
root.mainloop()
|
Posts: 157
Threads: 47
Joined: Nov 2021
(Aug-11-2024, 01:35 AM)menator01 Wrote: I altered the code a little. I disabled the button if the popup is open. When the popup is closed, the button will enable again.
You could also use withdraw and deiconify if wanted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import tkinter as tk
class Data:
var = 'Edit Here'
class Popup:
def __init__( self ):
self .window = tk.Toplevel( None )
self .window.geometry( '400x200+300+300' )
self .window.configure(pady = 20 , padx = 10 )
self .var = tk.StringVar()
self .entry = tk.Entry( self .window, textvariable = self .var)
self .entry.pack(fill = 'x' )
self .entry.focus()
class Window:
def __init__( self , parent):
self .parent = parent
self .parent.geometry( '200x200' )
self .parent.configure(pady = 20 )
self .button = tk.Button(parent, text = 'Open Window' )
self .button.pack()
class Controller:
def __init__( self , data, window, popup):
self .data = data
self .window = window
self .popup = popup
self .window.button.configure(command = self .openwindow, cursor = 'hand2' )
def openwindow( self ):
self .field = self .popup()
self .field.entry.insert( 'end' , self .data.var)
self .field.var.trace( 'w' , lambda var,index,mode: self .callback( self .field.var))
self .window.button.configure(state = 'disabled' , cursor = 'pirate' )
self .field.window.protocol( 'WM_DELETE_WINDOW' , self .enable)
def callback( self , var, index = None , mode = None ):
self .data.var = var.get()
def enable( self ):
self .field.window.destroy()
self .window.button.configure(state = 'normal' , cursor = 'hand2' )
if __name__ = = '__main__' :
root = tk.Tk()
controller = Controller(Data(), Window(root), Popup)
root.mainloop()
|
your code is flawless,
but i don't understand how it work,
i give you a reputation point
Posts: 1,145
Threads: 114
Joined: Sep 2019
What part you not understand and will try to explain?
Posts: 157
Threads: 47
Joined: Nov 2021
(Aug-11-2024, 03:33 AM)menator01 Wrote: What part you not understand and will try to explain? sorry it very basic, i don't understand class,
i learn java decade ago but never use it again since then,
the parts that confuse me is this
''' Controller class for communications between classes '''
how those thing communicated,
can it work without make it class?
Posts: 1,145
Threads: 114
Joined: Sep 2019
I'm sure it can. I haven't done any procedural programming in a long time. I found that it's usually easier to use classes (for me anyway) All the controller class does is (in this case) get data from the popup window and store it in the data class and pulls the variable from the data class and inserts into the entry in the popup window. It also enables and disables the button in the window class while the popup window is opened or closed.
|