Issue
Sorry,this may be a beginner question but i didn't seem to find my answer.
I have 2 python modules : source.py and work.py. I want to use source.py as a module where to define some tkinter functions and then use them in work.py.
In source.py i created a combobox function that memorises the desired option in a variable called "e". Variable "e" is declared global.
If i run the combobox function in the source.py module as "main", variable e will be changed everytime i select another option from the combobox. It works as expected
However, if i import the combobox in the second module "work.py". then the variable e is not changed but remains at the initial assigned value .
How can i change the script so that "e" will get changed when calling combobox function from work.py?
My modules below:
source.py:
from tkinter import *
from PIL import ImageTk, Image
from tkinter import ttk
def add_combo_box(root,lst,row,column):
global e
def comboclick(event):
global e
e=myCombo.get()
#myLabel = Label(root, text=myCombo.get())
#myLabel.pack()
print('e from combobox: ',e)
# create dropdown box
options = lst
myCombo = ttk.Combobox(root, value=options, width=40)
#myCombo.current(0)
myCombo.bind("<<ComboboxSelected>>", comboclick)
myCombo.grid(row=row,column=column)
#when running here it works ok. Variable "e" is getting changed
if __name__=='__main__':
root = Tk()
e="x"
options = ['one', 'two', 'three', 'four']
add_combo_box(root,options,row=1,column=1)
def display():
global e
messagebox.showinfo("", str(e))
btn=Button(root,text='show value',command=display)
btn.grid(row=2,column=1)
root.mainloop()
work.py:
from tkinter import *
from PIL import ImageTk,Image
from tkinter import ttk
from source import add_combo_box
from tkinter import messagebox
# Here it doesn't work. Combobox is launching. Variable "e" is getting initialised with "y" value but after calling the combobox function it stays the same
if __name__=='__main__':
root = Tk()
global e
e = "y"
options = ['one', 'two', 'three', 'four']
add_combo_box(root, options, row=1, column=1)
def display():
global e
messagebox.showinfo("", str(e))
btn = Button(root, text='show value', command=display)
btn.grid(row=2, column=1)
root.mainloop()
Solution
Your question remains a mystery to me, but I'm assuming you were having trouble with getting the value of the Combobox
made in the function, inside of source.py. You could just return the Combobox
from the function and assign a variable to it, and use the get()
method on it, like:
def add_combo_box(root,lst,row,column):
def comboclick(event):
e=myCombo.get()
#myLabel = Label(root, text=myCombo.get())
#myLabel.pack()
print('e from combobox: ',e)
# create dropdown box
myCombo = ttk.Combobox(root, value=lst, width=40)
#myCombo.current(0)
myCombo.bind("<<ComboboxSelected>>", comboclick)
myCombo.grid(row=row,column=column)
return myCombo #return the combobox
Inside work.py:
if __name__=='__main__':
root = Tk()
e = "y"
options = ['one', 'two', 'three', 'four']
ahem = add_combo_box(root, options, row=1, column=1) #assign a variable to it
def display():
messagebox.showinfo("", str(ahem.get())) #use the get on the variable to get the value of the combobox made
btn = Button(root, text='show value', command=display)
btn.grid(row=2, column=1)
root.mainloop()
Why did this not work with e
? The e
created in source.py and work.py are different and not the same. Indeed, I think the right way to access the properties of Combobox
is like I said.
Answered By - Delrius Euphoria
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.