将单个按钮用于多个任务

问题描述 投票:-1回答:1

我正在尝试检测,选择了哪个选项卡。返回选择的选项卡,“添加”按钮将打开相应的窗口。

我具有if语句的单独功能,以确保单击按钮会打开相应的窗口。但是,它不像我希望的那样工作。

无论选择哪个标签,它都会打开3个标签中的2个。

这里是功能代码:

def tab_add_btn():
    if tab_parent.index(active_business):
        nieuwe_zaken.business()
    if tab_parent.index(noc):
        noc_info.add_noc_info()
    if tab_parent.index(caller):
        Form.add_caller()

应用程序正在从项目中的不同.py文件调用其他窗口。

无论我打开了哪个选项卡,每次单击按钮都会打开add_noc_info和add_caller函数。即使我想在打开的选项卡上打开相应的功能。

我尝试了几种不同的语句/函数,但没有成功。

# The function which is called by the button
def tab_add_btn():
    if tab_parent.index(active_business):
        nieuwe_zaken.business()
    if tab_parent.index(noc):
        noc_info.add_info()
    if tab_parent.index(caller):
        Form.add_caller()

# The button
add = ttk.Button(main_window, text = "Add", command = tab_add_btn)
add.place(x = 1093, y = 495)

我希望按钮打开相应的窗口(功能),具体取决于打开/选择的选项卡。

如果你们需要更多代码段,请告诉我。

提前感谢!

python user-interface button tkinter
1个回答
0
投票

找到解决方案:

def tab_add_btn():
    tabid = tab_parent.index(tab_parent.select())

    if tabid == 0:
        nieuwe_zaken.business()
        print("Window opened | Niewe Zaken")
    elif tabid == 1:
        noc_info.add_noc_info()
        print("Window opened | NOC Info")
    elif tabid == 2:
        Form.add_caller()
        print("Window opened | Bellers Form")
    else:
        return 0

我必须先获取tab_id,然后才能构建if语句。

© www.soinside.com 2019 - 2024. All rights reserved.