lundi 29 avril 2019

How to use If-Statement inside the command of tkinter Button, to check that tkinter CheckButton is click or not?

Program Summary

I'm making a python based text processing program. Here I am using following Tkinter widgets with following names for its GUI:

  1. Text widget > "text_input"
  2. Text widget > "text_output"
  3. CheckButton widget > "checkButton_tense"
  4. Button widget > "button_analyse"

And Now I wanted to my program to run in following sequence.

  1. Taking text from "text_input".
  2. Perform the some Basic Text Processing in some other class on clicking the "button_analyse" and show output in "text_output".
  3. But if "checkButton_tense" widget is clicked/checked. Then it should also perform Tense Inspection along with basic text processing, else it should only perform basic text processing.
  4. Above operation no. 3 should be performed after checking status of "checkButton_tense" widget on clicking the "button_analyse" and show output in "text_output"

Problem/Error

Now when I use the if statement inside the "command=lambda:" of the my Tkinter Button "button_analyse" widget to check the status of "checkButton_tense" it gives me error. I have tried to do it with many ways but it don't works.

I have tried the solution mentioned here. But when I tried any solution like this i am unable to show my text in "text_output" widget because it is inside the different python method "main_gui_layout". I have also tried many other solutions given here at stack-overflow but don't found any identical. Kindly guide me in context of above mentioned problem and following code.

Code

from tkinter import *
from Examples import Examples
from TextAnalysis import PerformTextAnalysis


class MyGui:
    useExamples = Examples()
    performTextAnalysis = PerformTextAnalysis()

def __init__(self, master):
    self.main_gui_layout(master)

def main_gui_layout(self, master):

    # InputBox to get the input Text.

    text_input = Text(master, bd=1, height=15, width=100)
    text_input.insert('end', self.useExamples.example2)
    text_input.pack(side=TOP)

    # OutputBox to show the processed Text.

    text_output = Text(master, bd=1, height=15, width=100)
    text_output.pack(side=TOP)

    # CheckButton: it will perform tense analysis if checked/clicked

    tenseCheck_clicked = IntVar()
    checkButton_tense = Checkbutton(master, text="Tense Inspection", variable=tenseCheck_clicked,
                                    bg='#9aa7bc')
    checkButton_tense.var = tenseCheck_clicked
    checkButton_tense.pack(side=TOP)

    # Analysis Button: it will process text and show in output.
    # It will also perform Tense Analysis if Above "checkButton_tense" is check/active.

    button_analyse = Button(master, text="Analyse Requirements", width=20,
                            command=lambda:
                            [
                                self.performTextAnalysis.get_userReqiurement(
                                str(text_input.get('1.0', 'end'))),
                                if tenseCheck_clicked == 1:
                                    ans = self.performTextAnalysis.performBasicAnalysis(),
                                    self.performTextAnalysis.performTenseAnalysis(),
                                    text_output.insert(END, ans)
                                else:
                                self.performTextAnalysis.performBasicAnalysis()

                            ])
    button_analyse.pack(side=TOP)

Aucun commentaire:

Enregistrer un commentaire