'Method insert() for python tkinter function
I'm using Tkinter for developing a tool and in this function I need to set the value of 2 Entry of a different class (Toplevel2) but it seems that every time that i use set() or insert() methods the previous Entry widgets are cleared.
You know how to fix this problem?
PS. I've created a working demo code with two different scripts (maincode and maincode_support).
the first contains two classes of the first and second windows respectively with declared inside the various objects pertaining to that window. The second defines the command functions used in maincode.py
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import maincode_support
class Toplevel2_1:
def __init__(self, top=None):
self.top = top
self.combobox = tk.StringVar()
self.selected = tk.StringVar()
self.selected =[' Generic ',
' Acacia ',
' Beech ',
' Birch ',
' Cottonwood ',
' Douglas Fir '
]
self.Button1 = tk.Button(self.top)
self.Button1.place(relx=0.7, rely=0.748, height=24, width=127)
self.Button1.configure(command = lambda : maincode_support.save(self) )
self.Button1.configure(text='''Save''')
self.Entry1 = tk.Entry(self.top)
self.Entry1.place(relx=0.45, rely=0.249, height=20, relwidth=0.14)
self.Entry2 = tk.Entry(self.top)
self.Entry2.place(relx=0.45, rely=0.388, height=20, relwidth=0.14)
self.TCombobox1 = ttk.Combobox(self.top)
self.TCombobox1.place(relx=0.45, rely=0.526, relheight=0.058
, relwidth=0.138)
self.TCombobox1.configure(textvariable=self.combobox)
self.TCombobox1.configure(values = self.selected)
class Toplevel2:
def __init__(self, top=None):
self.top = top
self.selected = tk.StringVar()
self.combobox = tk.StringVar()
self.selected =[' Generic ',
' Acacia ',
' Beech ',
' Birch ',
' Cottonwood ',
' Douglas Fir '
]
self.Button1 = tk.Button(self.top)
self.Button1.place(relx=0.7, rely=0.748, height=24, width=127)
self.Button1.configure(text='''Toplevel2''')
self.Button1.configure(command = maincode_support.bind_top2)
self.Entry1 = tk.Entry(self.top)
self.Entry1.place(relx=0.45, rely=0.249, height=20, relwidth=0.14)
self.Entry2 = tk.Entry(self.top)
self.Entry2.place(relx=0.45, rely=0.388, height=20, relwidth=0.14)
self.TCombobox1 = ttk.Combobox(self.top)
self.TCombobox1.place(relx=0.45, rely=0.526, relheight=0.058
, relwidth=0.138)
self.TCombobox1.configure(textvariable=self.combobox)
self.TCombobox1.configure(values = self.selected)
def start_up():
maincode_support.main()
if __name__ == '__main__':
maincode_support.main()
maincode_support.py
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import maincode
def main(*args):
'''Main entry point for the application.'''
global root
root = tk.Tk()
root.protocol( 'WM_DELETE_WINDOW' , root.destroy)
# Creates a toplevel widget.
global _top1, _w1
_top1 = root
_w1 = maincode.Toplevel2(_top1)
root.mainloop()
def bind_top2(*args):
print('MakeCh4ngev1_support.bind_bio')
global _top2, _w2
_top2 = tk.Toplevel(root)
_w2 = maincode.Toplevel2_1(_top2)
def save(self):
global _top1, _top2, var1, var2, selection
selection = self.TCombobox1.get()
var1 = self.Entry1.get()
var2 = self.Entry2.get()
maincode.Toplevel2(_top1).TCombobox1.set(selection)
maincode.Toplevel2(_top1).Entry1.insert(0,var1)
maincode.Toplevel2(_top1).Entry2.insert(0,var2)
_top2.withdraw()
if __name__ == '__main__':
maincode.start_up()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
