首页 > 解决方案 > 单击后更改或删除链接到按钮的命令

问题描述

和 Tkinter 一起玩,遇到了一些麻烦。我有 3 个按钮:

Button1 将使用 Selenium 打开我指定的网站。

Button3 将在网站上执行我的功能。

Button2 将关闭程序。

Problem: I want to keep button1 from opening multiple windows/websites by 
either removing/hiding it after it has been clicked or changing the 
command 
that will be performed by it.



   After some googling I found some options and tried the following:
   | pack_forget()
   | pack_destroy()
   | btn1.destroy()

以上所有似乎都没有做任何事情。我错过了什么?

    import selenium
    from selenium import webdriver
    from time import sleep
    import time
    import tkinter
    from tkinter import *

    main = Tk()

    def Primary():
        txt = mtxt.get()
        New = Label(main, text=""+txt, fg='Blue').pack(side=BOTTOM)
        btn1 = Button(Bot, text='OK', command=Primary, 
        fg='Green').pack_destroy() #For some reason this does not work
        return

    def Secondary():
        global main
        main.destroy()

    def Tres():
        txt = mtxt.get()
        New = Label(main, text=""+txt, fg='Blue').pack(side=BOTTOM)
        return

    main.geometry('350x400+500+300')
    main.title('Title')

    mtxt = StringVar()

    Top = Frame().pack()
    Bot = Frame().pack(side=BOTTOM)

    Lbl = Label(main, text='TEXT').pack()
    Lbl2 = Label(main, text='TEXT').pack()

    Enter = Entry(main, textvariable=mtxt).pack()

    btn1 = Button(Bot, text='OK', command=Primary, fg='Green').pack()
    btn2 = Button(Bot, text='Cancel', command=Secondary, 
    fg='Red').pack(side=BOTTOM)
    btn3 = Button(Bot, text='Next', command=Tres, fg='Green').pack()

    main.mainloop()

预期结果:初始点击时 btn1 执行“command=Primary”

初始点击后 btn1 执行 'command=Tres'

或者

初始点击后,btn1 被破坏/隐藏,btn3 取而代之。

标签: python-3.xtkinter

解决方案


推荐阅读