Home > OS >  Send Tkinter Text to another function with button
Send Tkinter Text to another function with button

Time:11-04

I create a tkinter window ('window3' as shown) by a button trigger. I want have a function that takes data from 'KonuIcerik' ScrolledText on 'window3' then sends it to function 'def Finish():'

I know my problem is about using functions but i couldnt fix it. I need a little help

Error: "KonuIcerik" is not defined

    def KonuOlusturButtonu():
        window3= Toplevel(root)
        window3.geometry(f'{root_width}x{root_height} {center_x} {center_y}')
        window3.title("Konu Oluştur")
        window3.resizable(False, False)
        window3.configure(background='#0a0a0a')

        # Create ScrolledText widget
        KonuIcerik = ScrolledText(window3, width=96,  height=30)
        KonuIcerik.place(relx=0.5, rely=0.55, anchor=CENTER)

        # Create Button widget
        button_konubitir = Button(window3, text ="Bitir ve Yayınla", command = Finish())
        button_konubitir.config( height = 2, width = 30 )
        button_konubitir.place(relx=0.5, rely=0.92, anchor=CENTER)

    def Finish():
        KonuIcerikText = KonuIcerik.get()
        print(KonuIcerikText);

CodePudding user response:

First change the function to accept an argument:

def Finish(widget):
    KonuIcerikText = widget.get('0.0', 'end')
    print(KonuIcerikText);

Then change the command by creating a lambda to call it later and by passing the widget argument to be used by the function to get the text:

button_konubitir = Button(window3, text="Bitir ve Yayınla", command=lambda: Finish(KonuIcerik))

See also:

  • Why is the command bound to a Button or event executed when declared?

  • I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case, class names in CapitalCase. Don't have space around = if it is used as a part of keyword argument (func(arg='value')) but have space around = if it is used for assigning a value (variable = 'some value'). Have space around operators ( -/ etc.: value = x y(except here value = x y)). Have two blank lines around function and class declarations. Class methods have one blank line around them.

  • Related