Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of actively executing command function when button button is not pressed in Tkinter

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is a detailed introduction to "how to solve the problem that the button button in Tkinter is not pressed but the command function is actively executed". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to solve the problem that the button button in Tkinter is not pressed but the command function is actively executed" can help you solve your doubts. The following is a detailed introduction to learn new knowledge together with the ideas of the editor.

preface

When using Tkinter as an interface, I encountered such a problem:

The program has just run, the button has not been pressed, but the button response function has already run.

For example, the following program: from Tkinter import *class App: def __init__(self,master): frame = Frame(master) frame.pack() Button(frame,text='1', command = self.click_button(1)).grid (row=0,column=0) Button (frame,text='2', command = self.click_button(2)).grid (row=0,column=1) Button (frame,text='3', command = self.click_button(1)).grid (row=0,column=2) Button (frame,text='4', command = self.click_button(2)).grid (row=1,column=0) Button (frame,text='5', command = self.click_button(1)).grid (row=1,column=1) Button(frame,text='6', command = self.click_button(2)).grid(row=1,column=2) def click_button(self,n): print 'you clicked:', n root=Tk()app=App(root)root.mainloop() As soon as the program runs, the following occurs:

None of the six buttons were pressed, but the command function was already running.

Later through the Internet search, found that the cause of the problem is the command function with parameters caused

tkinter requires that controller functions triggered by buttons (or other plug-ins) must not contain parameters

To pass parameters to a function, you need to add lambda before the function.

From Tkinter import *class App: def __init__(self,master): frame = Frame(master) frame.pack() Button(frame,text='1', command = lambda: self.click_button(1)).grid (row=0,column=0) Button (frame,text='2', command = lambda: self.click_button(2)).grid (row=0,column=1) Button (frame,text='3', command = lambda: self.click_button(1)).grid (row=0,column=2) Button (frame,text='4', command = lambda: self.click_button(2)).grid (row=1,column=0) Button (frame,text='5', command = lambda: self.click_button(1)).grid (row=1,column=1) Button(frame,text='6', command = lambda: self.click_button(2)).grid(row=1,column=2) def click_button(self,n): print 'you clicked :',n root=Tk()app=App(root)root.mainloop()

Supplement: The Tkinter Button component calls a function with an argument

Here we're going to use Python's lambda function, lambda is to create an anonymous function with a colon preceded by an incoming argument followed by a single-line expression that handles the incoming argument.

Calling lambda functions returns the result of an expression.

First let's create a function fun(x):def fun(x): print x

Then let's create a Button: (here omitting the sequence of code that calls Tkinter, just writing the important part)

Button(root, text ='Button ', command=lambda :fun(x)) Let's create a variable x=1:x = 1

Click on this Button and it will print 1.

Read here, this article "how to solve the Tkinter button does not press but actively execute the command function" article has been introduced, want to master the knowledge points of this article also need to be used by ourselves to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report