D
D
DanOK172021-12-03 10:19:22
Python
DanOK17, 2021-12-03 10:19:22

Why is tkinter not updating screen text?

It is necessary that the text in the text window changes to the one that will be in the TextR variable, but this does not happen.

from random import choice
from tkinter import *
import keyword


root = Tk()

TextR = 'Вас приветсвует программа по созданию паролей созданный DanOK17 \nYou are welcomed by the password creation program created by DanOK17\n\nНапишите russian если хотите продолжить на русском \nWrite english if you want to continue in English\n'
TextY = 'Yes'
TextN = 'No'
txt = StringVar()


def click():
    txt.get()
    global TextR
    print('1')
    if txt == 'russian':
        TextR='Введите длинну пароля'
        title.config(text=TextR)
    elif txt == 'english':
        TextR='Enter password length'
        title.config(text=TextR)


root['bg'] = '#fafafa'
root.title('PGen')
root.wm_attributes('-alpha', 0.9)
root.geometry('700x500')

frametxt = Frame(root, bg='grey')
frametxt.place(relheight=0.6, relwidth=1)

frame_btn = Frame(root, bg='grey', bd=5)
frame_btn.place(relheight=0.4, relwidth=1, rely=0.60)


title = Label(frametxt, text=TextR, bg='white', font=40)
title.pack(fill=BOTH, expand=True, side=TOP)
ent = Entry(frame_btn, bg='white', font=100, bd=2, textvariable=txt)
ent.pack(fill=BOTH, pady=2)
btn_ent = Button(frame_btn, text='Ввести', font=100, bd=2, height=5, command=click())
btn_ent.pack(side=RIGHT, pady=2,padx=2)
btnY = Button(frame_btn, text=TextY, font=100, bd=2, width=30, height=5)
btnY.pack(pady=2, side=LEFT)
btnN = Button(frame_btn, text=TextN, font=100, bd=2, width=30, height=5)
btnN.pack(pady=2, side=RIGHT)

if txt == 'russian':
    TextR = 'Введите длинну пароля: '

root.mainloop()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-12-03
@Vindicar

Because you don't know what you're doing.

btn_ent = Button(frame_btn, text='Enter', font=100, bd=2, height=5, command= click() )

You assign the result of calling the click() function as the button click handler. It doesn't have any return, so it returns None.
If you wanted to assign click() as a handler, then you had to specify the function itself, and not the result of its call.
command=click

Next, comparison
txt == 'something'
incorrect, since StringVar() and str are different data types and they won't be equal. Use
txt.get() == 'whatever'
. A lone call to txt.get() at the beginning of click() is useless because you're ignoring the return value of get() .
Actually, I don't see where you initialize the txt variable. You don't have any txt.set() calls.
Hell, assigning TextR before root.mainloop() won't affect anything since it was used before.
If you want the variable assignment to be immediately reflected in the controls, you need to use another StringVar(), and call its .set() method to change the value.

D
Daniil Shevkunov, 2021-12-03
@danila763

1) A function must be passed to the command argument, not the result of its execution

# Не так
btn_ent = Button(command=click())
# А так
btn_ent = Button(command=click)

2) txt.get() Returns the text, it must be assigned somewhere
# Не так
def click():
    txt.get()
    global TextR
    print('1')
    if txt == 'russian':
        TextR='Введите длинну пароля'
        title.config(text=TextR)
    elif txt == 'english':
        TextR='Enter password length'
        title.config(text=TextR)

# А так
def click():
    text = txt.get()
    global TextR
    if text == 'russian':
        TextR='Введите длину пароля'
    elif text == 'english':
        TextR='Enter password length'
    title.config(text=TextR)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question