简体   繁体   中英

How to control the events of clicking on a button in tkinter

I want to ask that if I click on a button, then it perform a command and if I click again on the button then it perform another command. Basically I have two commands which I want to use in loop.

    #Import tkinter here
    from tkinter import *
    #defining root function.
    root = Tk()
    root.geometry('200x100')
    #define fuction for labelA
    def funcA():
        labelA["text"] = "A responds"
    #define fuction for labelB
    def funcB():
        labelB["text"] = "B responds"
    #Define button.
    button = Button(root, text = 'Click Me', command=lambda:[funcA(), funcB()])
    button.pack()
    #creating both label A and B
    labelA = Label(root, text="A")
    labelB = Label(root, text="B")
    labelA.pack()
    labelB.pack()
    root.mainloop()

In this code when I click on button, both the function run at same time. But I want that when i click on the button, the first function should run and if I click again on button then run second function. This will be in loop (like first of all first label will update then second label will update then again first label update then again second label).

i think you can save the button inside a variable and when function1 run after execution set command attribute to function2 and after function2 executed set command to function1

You should have a bool value tracking if the button has been pressed or not. You can change this value in your functions as well.

somebool = False
#define fuction for labelA
def funcA():
    if somebool:
        labelA["text"] = "A responds"
#define fuction for labelB
def funcB():
    if !somebool:
        labelB["text"] = "B responds"

Note that this approach is not good if you want to access one function alone from another button. If you have a bool state like this though, you do not need 2 separate functions. You can make an event handler for the button and then call funcA or funcB from there.

somebool = False
def handler():
    funcA() if somebool else funcB()
def funcA():
    labelA["text"] = "A responds"
def funcB():
    labelB["text"] = "B responds"
button = Button(root, text = 'Click Me', command=handler)

If you don't care about reusing the functions, change the command attribute for the button in your function to run the other one.

IIUC you want to alternate between two (or more) functions when clicking a button. So you can use itertools.cycle (built-in) to create an iterable that will loop forever over the given items in a tuple or list or sth else. So you add the two functions to the cycle and the button calls a third function that just gets the next item in the cycle and calls it and so on. (also I used just one label so that change is always visible since in your case you wouldn't see any change, but I believe you can adapt these functions for your needs):

import tkinter as tk
import itertools


def func_a():
    label.config(text='a responds')


def func_b():
    label.config(text='b responds')


toggle_funcs = itertools.cycle((func_a, func_b))


def toggle():
    func = next(toggle_funcs)
    func()


root = tk.Tk()

label = tk.Label(root, text='None')
label.pack()

btn = tk.Button(root, text='Click Me', command=toggle)
btn.pack()

root.mainloop()

Also:
I strongly advise against using wildcard ( * ) when importing something, You should either import what You need, eg from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.

I strongly suggest following PEP 8 - Style Guide for Python Code . Function and variable names should be in snake_case , class names in CapitalCase . Have two blank lines around function and class declarations. Object method definitions have one blank line around them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM