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

标签 python tkinter button tkinter.optionmenu

我有一个列表和 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"

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

这是我的代码:

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

这将解决您的问题

关于python - 如何根据 tkinter 菜单中选定的选项触发按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67701045/

相关文章:

Python Tkinter : How to get status message about menu action?

javascript - 如何使 jQuery 表格单元格 (td) 可单击以运行函数?

python - Django 中的菜单选项卡突出显示问题

python - Regex Python - 修改随机生成的字符串

Python Tkinter : TypeError: 'Frame' object is not callable

wpf - 使用触发器更改内容按钮

android - 在按钮的一角添加文本 Android

python - 无法在 Cython 中使用命名空间返回 C++ 代码中的 C 结构

python - 函数内的for循环

fonts - 带有粗体和普通文本的 Python tkinter 单标签