Home > database >  I'm unable to get a string out of tkinter entrybox
I'm unable to get a string out of tkinter entrybox

Time:02-16

import random
import tkinter as tk

frame = tk.Tk()
frame.title("koeweils baldadige encyptor")
frame.geometry('400x200')

printButton = tk.Button(frame,text = "Print", command = lambda: zandkasteel())
printButton.pack()

freek = tk.Text(frame,height = 5, width = 20)
freek.pack()

input_a = freek.get(1.0, "end-1c")

print(input_a)

fruit = 0

fad = input_a[fruit:fruit 1]

print(fad)

schepje = len(input_a.strip("\n"))

print(schepje)

def zandkasteel():
    lbl.config(text = "Ingevulde string: " input_a)
    with open("luchtballon.txt", "w") as chocoladeletter:
        for i in range(schepje):
            n = random.randint()
            print(n)
            leuk_woord = ord(fad)*n
            print(leuk_woord)
            chocoladeletter.write(str(leuk_woord))
            chocoladeletter.write(str(n))
            chocoladeletter.write('\n')

lbl = tk.Label(frame, text = "")
lbl.pack()

frame.mainloop()

I need to get the string that was entered into the text entry field freek. I have tried to assign that string to input_a, but the string doesn't show up.

Right now, input_a doesn't get anything assigned to it and seems to stay blank. I had the same function working before implementing a GUI, so the problem shouldn't lie with the def zandkasteel.

To be honest I really don't know what to try at this point, if you happen to have any insights, please do share and help out this newbie programmer in need.

CodePudding user response:

Here are some simple modifications to your code that shows how to get the string in the Text widget when it's needed — specifically when the zandkasteel() function gets called in response to the user clicking on the Print button.

import random
import tkinter as tk


frame = tk.Tk()
frame.title("koeweils baldadige encyptor")
frame.geometry('400x200')

printButton = tk.Button(frame, text="Print", command=lambda: zandkasteel())
printButton.pack()

freek = tk.Text(frame, height=5, width=20)
freek.pack()

def zandkasteel():
    input_a = freek.get(1.0, "end-1c")
    print(f'{input_a=}')
    fruit = 0
    fad = input_a[fruit:fruit 1]
    print(f'{fad=}')
    schepje = len(input_a.strip("\n"))
    print(f'{schepje=}')

    lbl.config(text="Ingevulde string: "   input_a)
    with open("luchtballon.txt", "w") as chocoladeletter:
        for i in range(schepje):
            n = random.randint(1, 3)
            print(n)
            leuk_woord = ord(fad)*n
            print(leuk_woord)
            chocoladeletter.write(str(leuk_woord))
            chocoladeletter.write(str(n))
            chocoladeletter.write('\n')

lbl = tk.Label(frame, text="")
lbl.pack()

frame.mainloop()

  • Related