I have a script that uses tkinter. I have created predefined radio buttons in this script. I am trying to call the script from a second script. When I run the original script the print out works fine. However, when I run it from the second script, the radio button var.get() variable don't seem to be translating between scripts and the print out is not as expected. What am I doing wrong? Any help with this would be greatly appreciated!!
Ex. when I run from first script the print out is:
You've selected Arctic
You've selected Forest
You've selected Grassland
You've selected Mountain
when I run from the second scrip the print out is:
You've selected
You've selected
You've selected
You've selected
--the radio button selection is missing
Additional details: Both scripts are saved in the same folder. I have tried setting "selection" as a global variable.
To reproduce this error:
1.save as two separate python scripts (first script, second script) within the same folder.
Run the first script, make a radio selection, and the print message will be as expected. ex "You've selected Arctic"
Run the second script, select "create" in the menu. Select "Get Monsters". Make a radio button selection. The print out will only be "You've selected" It does not include the biome
First Script:
from tkinter import *
def sel():
selection = "You've selected " + var.get()
poop = var.get()
print(selection)
return poop
root = Tk()
var = StringVar()
radio_frame = Frame(root, borderwidth=2,relief="groove")
## Radio buttons for choosing biome
biome_label=Label(radio_frame, text="Please choose a biome")
biome_label.pack()
search_biome1 = Radiobutton(radio_frame, text="Arctic", variable=var,
value="Arctic", command=sel, width=10, anchor=W)
search_biome1.pack()
search_biome2 = Radiobutton(radio_frame, text="Coast", variable=var,
value="Coast", command=sel, width=10, anchor=W)
search_biome2.pack()
search_biome3 = Radiobutton(radio_frame, text="Desert", variable=var,
value="Desert", command=sel, width=10, anchor=W)
search_biome3.pack()
search_biome4 = Radiobutton(radio_frame, text="Forest", variable=var,
value="Forest", command=sel, width=10, anchor=W)
search_biome4.pack()
search_biome5 = Radiobutton(radio_frame, text="Grassland", variable=var,
value="Grassland", command=sel, width=10, anchor=W)
search_biome5.pack()
search_biome6 = Radiobutton(radio_frame, text="Hill", variable=var,
value="Hill", command=sel, width=10, anchor=W)
search_biome6.pack()
search_biome7 = Radiobutton(radio_frame, text="Mountain", variable=var,
value="Mountain", command=sel, width=10, anchor=W)
search_biome7.pack()
search_biome8 = Radiobutton(radio_frame, text="Swamp", variable=var,
value="Swamp", command=sel, width=10, anchor=W)
search_biome8.pack()
search_biome9 = Radiobutton(radio_frame, text="Underdark", variable=var,
value="Underdark", command=sel, width=10, anchor=W)
search_biome9.pack()
search_biome10 = Radiobutton(radio_frame, text="Underwater", variable=var,
value="Underwater", command=sel, width=10, anchor=W)
search_biome10.pack()
search_biome11 = Radiobutton(radio_frame, text="Urban", variable=var,
value="Urban", command=sel, width=10, anchor=W)
search_biome11.pack()
radio_frame.grid()
root.mainloop()
Second Script:
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("Main")
def getMonsters():
import DD_Enemy_Generator_Biome
poop = DD_Enemy_Generator_Biome.sel()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Create", menu=filemenu)
filemenu.add_command(label="Get Monsters", command=getMonsters)
root.config(menu=menubar)
root.mainloop()
change the way var.get works for this.. also, use lambda in the command and it works...
def sel(biome_):
global selection
selection = "You've selected " + biome_
poop = biome_
print(selection)
return poop
root = Tk()
var = StringVar()
radio_frame = Frame(root, borderwidth=2,relief="groove")
## Radio buttons for choosing biome
biome_label=Label(radio_frame, text="Please choose a biome")
biome_label.pack()
search_biome2 = Radiobutton(radio_frame, text="Coast", variable=var,
value="Coast", command=lambda value="Coast": sel(value), width=10, anchor=W)