如何根据tkinkter菜单中的选定选项触发按钮?

问题描述

我有一个列表和 3 条路径。每条路径都指向一个专为该国家/地区运行的应用程序。

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"

我创建的 Run button 需要根据我在 OptionMenu 中选择的国家/地区触发这 3 个应用程序之一。因此,如果我选择 Malaysia,我希望 Run buttonpath_malaysia 中运行应用程序。我正在努力解决这个问题。例如,如果我点击 Run button 中的 Run application Malaysia,我最好还希望 Malaysia 更改为 OptionMenu

这是我的代码

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()

现在它只在西班牙运行...

解决方法

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()

这将解决您的问题

,

你可以试试这个:

def run():
    if dd_owner.get() == "Spain":
        os.system(f'python "{path_spain}"') #=== Run the file by formatting the path into the string
    elif dd_owner.get() == "United Kingdom":
        os.system(f'python "{path_uk}"')  #=== Run the file by formatting the path into the string
    
    elif dd_owner.get() == "Malaysia":
        os.system(f'python "{path_malaysia}"')  #=== Run the file by formatting the path into the string

还要更改按钮文本:

def change_text(*args):
    if dd_owner.get() == "Spain":
        
        run_button.config(text="Run Application Spain") #== Configure button
    elif dd_owner.get() == "United Kingdom":
        
        run_button.config(text="Run Application United Kingdom") #== Configure button
    elif dd_owner.get() == "Malaysia":
        
        run_button.config(text="Run Application Malaysia") #== Configure button

还要记得添加这个代码:

dd_owner.trace("w",change_text) #=== Trace the stringVar for changes.

更改按钮代码。

将其放在 dd_owner.set(owner[0]) # the first value 下方

总而言之,代码如下:

import os
from tkinter import *

owner = ['Spain','Malaysia']

path_spain = "C:/data/FF/Desktop/PythonFolder/spain_software.py"
path_uk = "C:/data/FF/Desktop/PythonFolder/uk_software.py"
path_malaysia = "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":
        os.system(f'python "{path_spain}"')
        run_button.config(text="Run Application Spain")
    elif dd_owner.get() == "United Kingdom":
        os.system(f'python "{path_uk}"')
        run_button.config(text="Run Application United Kingdom")
    elif dd_owner.get() == "Malaysia":
        os.system(f'python "{path_malaysia}"')
        run_button.config(text="Run Application Malaysia")

def change_text(*args):
    if dd_owner.get() == "Spain":
        
        run_button.config(text="Run Application Spain")
    elif dd_owner.get() == "United Kingdom":
        
        run_button.config(text="Run Application United Kingdom")
    elif dd_owner.get() == "Malaysia":
        
        run_button.config(text="Run Application Malaysia")

# The run button (this button runs some other software)
dd_owner = StringVar(window)
dd_owner.set(owner[0])  # the first value

run_button = Button(window,text=f"Run application {dd_owner.get()}",row=2)

# These are the option menus
w = OptionMenu(window,*owner)
dd_owner.trace("w",change_text)
w.grid(row=0,column=1)

# These are the titles
l1 = Label(window,column=0)

window.mainloop()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...