1
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.

martineau
  • 119,623
  • 25
  • 170
  • 301
Skoelio
  • 13
  • 2
  • 2
    You're calling `.get` a few microseconds after the widget was created, before the user has even seen the widget. – Bryan Oakley Feb 14 '22 at 22:21
  • I suggest you read Bryan's answer to [this question](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time). – martineau Feb 14 '22 at 22:53
  • Remember that none of those statements actually does any drawing. All they do is queue up messages. The drawing doesn't happen until `frame.mainloop()` runs, and has a change to fetch and dispatch those messages. To read the value, you'll need to be in your own message handler, like for an "OK" button. – Tim Roberts Feb 14 '22 at 23:11

1 Answers1

0

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()

martineau
  • 119,623
  • 25
  • 170
  • 301
  • `input_a` is a local variable inside `show_freek()` and it cannot be accessed inside `zandkasteel()`. Also why don't call `input_a = freek.get(...)` inside `zandkasteel()` instead? – acw1668 Feb 15 '22 at 01:30
  • @acw1668: Because somehow I missed the fact that `input_a` was being used anywhere else — although it seems obvious now (and ditto for several other of the variables). Thanks for the heads-up. – martineau Feb 15 '22 at 02:50