InstantNut
InstantNut

Reputation: 93

How do I assign a function to a button so that when it clicks, it displays a string onto a Text() window?

I am trying to make a GUI program where I assign a function to a button so that when pressed it returns a text. However, I am having trouble doing so.

from tkinter import *

class PayrollSummary:
    def __init__(pay):
        window = Tk()
        window.title("Employee Payroll")

        #Add Frame 1
        frame1 = Frame(window)
        frame1.pack()


        #Add ReadFile Button          
        btReadFile = Button(frame1, text = "Read File")


        #Add ShowPayroll Button
        btShowPayroll = Button(frame1, text = "Show Payroll") #When I press the button "Show Payroll", I want it to display a text in the textbox in the frame below. I tried command = printPayroll but i dont think its working :(
        #printPayroll <- use this function to do so



        #Formatting
        btReadFile.grid(row = 1, column = 2, sticky="w")
        btShowPayroll.grid(row = 2, column = 2, sticky="w")


        #Text Window
        text = Text(window)
        text.pack()
        text.insert(END, "text displayed from btShowPayroll") #when btShowPayroll is pressed I want it to display text here!

        window.mainloop()

PayrollSummary()

Upvotes: 0

Views: 39

Answers (2)

FrainBr33z3
FrainBr33z3

Reputation: 1105

You need to add a command to the button click and move text.insert into a new function, as follows:

from tkinter import *

class PayrollSummary:
    def __init__(self):
        window = Tk()
        window.title("Employee Payroll")

        #Add Frame 1
        frame1 = Frame(window)
        frame1.pack()


        #Add ReadFile Button          
        btReadFile = Button(frame1, text = "Read File")


        #Add ShowPayroll Button
        btShowPayroll = Button(frame1, text = "Show Payroll", command = self.printPayroll) #When I press the button "Show Payroll", I want it to display a text in the textbox in the frame below. I tried command = printPayroll but i dont think its working :(
        #printPayroll <- use this function to do so



        #Formatting
        btReadFile.grid(row = 1, column = 2, sticky="w")
        btShowPayroll.grid(row = 2, column = 2, sticky="w")


        #Text Window
        self.text = Text(window)
        self.text.pack()

        window.mainloop()

    def printPayroll(self):
        self.text.insert(END, "text displayed from btShowPayroll \n") #when btShowPayroll is pressed I want it to display text here!

PayrollSummary()

Upvotes: 1

user11111136
user11111136

Reputation:

I have solved your problem.

To do this make a method/function of the PayrollSummary class called 'printpayroll' and make it insert the text 'text displayed from btShowPayroll' into the text box. Make the command of the button 'lambda:self.printpayroll()' so that it runs the method/function.


from tkinter import *

class PayrollSummary:
    def __init__(self):
        window = Tk()
        window.title("Employee Payroll")

        #Add Frame 1
        self.frame1 = Frame(window)
        self.frame1.pack()


        #Add ReadFile Button          
        self.btReadFile = Button(self.frame1, text = "Read File")

        #Text Window
        self.text = Text(window)
        self.text.pack()

        #Add ShowPayroll Button
        self.btShowPayroll = Button(self.frame1, text = "Show Payroll", command = lambda:self.printpayroll()) #When I press the button "Show Payroll", I want it to display a text in the textbox in the frame below. I tried command = printPayroll but i dont think its working :(
        #printPayroll <- use this function to do so

        #Formatting
        self.btReadFile.grid(row = 1, column = 2, sticky="w")
        self.btShowPayroll.grid(row = 2, column = 2, sticky="w")

        window.mainloop()

    def printpayroll(self):
        self.text.insert(END, "text displayed from btShowPayroll") #when btShowPayroll is pressed I want it to display text here!

PayrollSummary()

This code creates this window when run:

Window

This is what happens when the 'Show Payroll' button is clicked:

Result

As you can see, what you wanted to happen happened. The text is outputted onto the screen. This happens because the text is inserted into the text box.


Some recommendations:

  • I recommend you create the window of the program outside of the class because it would be better to make your program only have one window and then multiple frames for each GUI screen of the program.

  • I recommend adding self. before a lot of the tkinter widgets you created in your class so that they are created as attributes of objects that are created as instantiations of the class.

  • I recommend you change the code that runs the class to:

    PayrollSummaryScreen = PayrollSummary()
    

    I would do this so that the 'PayrollSummaryScreen' object is created and that each of the object's attributes that are created. e.g. frame1, btReadFile, btShowPayroll and text can be accessed using the object. For example you could hide the text if you wanted to, from outside of the class using:

    PayrollSummaryScreen.text.pack_forget()
    

    This could be useful for you in the future, for example if you need to hide the Payroll Summary screen and by hiding the 'text' text box and the 'Read File' and 'Show Payroll' buttons, you could hide the frame1 attribute of the object. This would therefore hide the frame's child-widgets which are the 'text' text box and the 'Read File' and 'Show Payroll' buttons. You could hide the frame1 attribute of the payroll summary screen object using:

    PayrollSummaryScreen.frame1.pack_forget()
    

If you were to take on these recommendations, this would be the code:


from tkinter import *

window = Tk()
window.title("Employee Payroll")

class PayrollSummary:
    def __init__(self):
        #Add Frame 1
        self.frame1 = Frame(window)
        self.frame1.pack()


        #Add ReadFile Button          
        self.btReadFile = Button(self.frame1, text = "Read File")

        #Text Window
        self.text = Text(window)
        self.text.pack()

        #Add ShowPayroll Button
        self.btShowPayroll = Button(self.frame1, text = "Show Payroll", command = lambda:self.printpayroll()) #When I press the button "Show Payroll", I want it to display a text in the textbox in the frame below. I tried command = printPayroll but i dont think its working :(
        #printPayroll <- use this function to do so

        #Formatting
        self.btReadFile.grid(row = 1, column = 2, sticky="w")
        self.btShowPayroll.grid(row = 2, column = 2, sticky="w")

        window.mainloop()

    def printpayroll(self):
        self.text.insert(END, "text displayed from btShowPayroll") #when btShowPayroll is pressed I want it to display text here!

PayrollSummaryScreen = PayrollSummary()

I hope this extra information helps!

Upvotes: 0

Related Questions