How to use If-Statement inside the command of tkinter Button, to check that tkinter CheckButton is click or not?
You should't be putting that sort of logic in a lambda
. The way tkinter is designed to work is that the button should call a function. Then it becomes trivial to add any logic you want.
You simply need to identify any widgets that need to be accessed in this function, and make them properties of the class.
In your case it would look something like this:
class MyGui: useExamples = Examples() performTextAnalysis = PerformTextAnalysis() def main_gui_layout(self, master): ... self.text_input = Text(master, bd=1, height=15, width=100) .... self.tenseCheck_clicked = IntVar() ... button_analyse = Button(master, text="Analyse Requirements", width=20, command=self.on_button_click) def on_button_click(self, event): data = self.text_input.get('1.0', 'end') self.performTextAnalysis.get_userReqiurement(data) if self.tenseCheck_clicked == 1: ans = self.performTextAnalysis.performBasicAnalysis() self.performTextAnalysis.performTenseAnalysis() self.text_output.insert(END, ans) else: self.performTextAnalysis.performBasicAnalysis()
I do not have your Examples
or TextAnalysis
libraries so I cannot test functionality of them however I can provide an example using your code that should be good enough to show what we mean by "Move the lambda function to its own function".
You had a few things I would change like your class for one. Your class is more complex then it needs to be so lets remove the main_gui_layout
method and just build the GUI in the init
method. That said we can also inherit from the Tk()
class so we can use self
for the container of the widgets and class attributes.
Also I would change your tkinter import to import tkinter as tk
as this will help prevent accidentally overwriting imports.
Se below example and let me know if you have any questions.
import tkinter as tkfrom Examples import Examplesfrom TextAnalysis import PerformTextAnalysisclass MyGui(tk.Tk): def __init__(self): super().__init__() self.useExamples = Examples() self.performTextAnalysis = PerformTextAnalysis() self.text_input = tk.Text(self, bd=1, height=15, width=100) self.text_input.insert('end', "self.useExamples.example2") self.text_input.pack(side='top') self.text_output = tk.Text(self, bd=1, height=15, width=100) self.text_output.pack(side='top') self.tenseCheck_clicked = tk.IntVar() checkButton_tense = tk.Checkbutton(self, text="Tense Inspection", variable=self.tenseCheck_clicked, bg='#9aa7bc') checkButton_tense.var = self.tenseCheck_clicked checkButton_tense.pack(side='top') tk.Button(self, text="Analyse Requirements", width=20, command=self.new_function).pack(side='top') def new_function(self): self.performTextAnalysis.get_userReqiurement(str(self.text_input.get('1.0', 'end'))) if self.tenseCheck_clicked == 1: ans = self.performTextAnalysis.performBasicAnalysis() self.performTextAnalysis.performTenseAnalysis() self.text_output.insert('end', ans) else: self.performTextAnalysis.performBasicAnalysis()MyGui().mainloop()