pythontkinterbuttontkinter.optionmenu

How to triger a button based on the selected option in menu in tkinkter?


I have a list and 3 paths. Every path refers to an application that runs specifically for that country.

country = ['Spain', 'United Kingdom', 'Malaysia']

path_spain = r"c:\data\FF\Desktop\PythonFolder\spain_software.py"
path_uk = r"c:\data\FF\Desktop\PythonFolder\uk_software.py"
path_malaysia = r"c:\data\FF\Desktop\PythonFolder\malaysia_software.py"

The Run button I created needs to trigger one of those 3 applications based on the country I select in the OptionMenu. So If I select Malaysia I want the Run button to run the application in the path_malaysia. I'm struggling to fix this. Preferably I would also like the Run button to change to Run application Malaysia if I click Malaysia in the OptionMenu for example.

This is my code:

import os
from tkinter import *
       
window = Tk()
window.title("Running Python Script") #Create window
window.geometry('550x300') #geo of the window

def run():
    os.system('python path_spain') 

#The run button (this button runs some other software)
run_button = Button(window, text="Run application.....", bg="blue", fg="white",command=run)
run_button.grid(column=0, row=2)

#These are the option menus
dd_country = StringVar(window)
dd_country.set(country [0]) #the first value
w = OptionMenu(window, dd_country, *country)
w.grid(row=0,column=1)

#These are the titles
l1 = Label(window,  text='Select Country', width=15 )  
l1.grid(row=0,column=0)

mainloop()

Right now it only runs for Spain...


Solution

  • import os
    from tkinter import *
    
    owner = ['Spain', 'United Kingdom', 'Malaysia']
    
    path_spain = r"c:\data\FF\Desktop\PythonFolder\spain_software.py"
    path_uk = r"c:\data\FF\Desktop\PythonFolder\uk_software.py"
    path_malaysia = r"c:\data\FF\Desktop\PythonFolder\malaysia_software.py"
    
    window = Tk()
    window.title("Running Python Script")  # Create window
    window.geometry('550x300')  # geo of the window
    
    
    def run():
        if dd_owner.get() == "Spain":
            print("spain")
            # os.system('python path_spain')
        elif dd_owner.get() == "United Kingdom":
            os.system('python path_uk')
    
        elif dd_owner.get() == "Malaysia":
            os.system('python path_malaysia')
    
    
    def update_button(_):
        run_button.config(text="Run application {}".format(dd_owner.get()))
    
    
    # The run button (this button runs some other software)
    
    
    # These are the option menus
    dd_owner = StringVar(window)
    dd_owner.set(owner[0])  # the first value
    w = OptionMenu(window, dd_owner, *owner, command=update_button)
    # w.config()
    w.grid(row=0, column=1)
    
    run_button = Button(window, text="Run application {}".format(dd_owner.get()), bg="blue", fg="white",command=run)
    run_button.grid(column=0, row=2)
    
    # These are the titles
    l1 = Label(window, text='Select Owner', width=15)
    l1.grid(row=0, column=0)
    
    mainloop()
    

    This will solve your problem