Не работает функция character_limit в дочернем окне Tkinter. from tkinter import * def re(): def character_limit1(entry_text1): if - вопрос №5021285

len(entry_text1.get()) > 0: entry_text1.set(entry_text1.get()[-1]) re() new_window1 = Tk() new_window1.title("(Текст2)") # название окна new_window1.geometry('830x880') # Размер окна new_window1.resizable(width=True, height=True) # Разрешить изменять размер окна frame12 = Frame(new_window1, bg=c) frame12.place(relx=0.0, rely=0.0, relwidth=1.0, relheight=1.0) canvas12 = Canvas(frame12, width=800, height=230, bg=c) canvas12.pack() entry = StringVar() # the text in your entry entry_widget2 = Entry(canvas12, width=20, textvariable=entry) # the entry canvas12.create_window(100, 100, window=entry_widget2) entry.trace(«w», lambda *args: character_limit1(entry)) # new_window1.mainloop, что есть, что нет, всё также def character_limit(entry_text): if len(entry_text.get()) > 0: entry_text.set(entry_text.get()[-1]) root = Tk() # окно root.title("(Текст1)") # название окна root.geometry('830x880') # Размер окна frame1 = Frame(root, bg=c) frame1.place(relx=0.0, rely=0.0, relwidth=1.0, relheight=1.0) canvas2 = Canvas(frame1, width=800, height=230, bg=c) canvas2.pack() entry_text = StringVar() # the text in your entry entry_widget = Entry(canvas2, width=20, textvariable=entry_text) # the entry canvas2.create_window(100, 100, window=entry_widget) entry_text.trace(«w», lambda *args: character_limit(entry_text)) re() root.mainloop()
03.01.23
1 ответ

Ответы

Проблема заключается в том, что функция character_limit1 вызывается внутри функции re, но re не вызывается нигде в коде. Вам нужно вызвать функцию re, чтобы она запустила функцию character_limit1 и настроила отслеживание изменений в entry. Вот исправленный код:

from tkinter import *

def character_limit1(entry_text1):
    if len(entry_text1.get()) > 0:
        entry_text1.set(entry_text1.get()[-1])

def re():
    new_window1 = Tk()
    new_window1.title("(Текст2)") # название окна
    new_window1.geometry('830x880') # Размер окна
    new_window1.resizable(width=True, height=True) # Разрешить изменять размер окна
    frame12 = Frame(new_window1, bg='white')
    frame12.place(relx=0.0, rely=0.0, relwidth=1.0, relheight=1.0)
    canvas12 = Canvas(frame12, width=800, height=230, bg='white')
    canvas12.pack()
    entry = StringVar() # the text in your entry
    entry_widget2 = Entry(canvas12, width=20, textvariable=entry) # the entry
    canvas12.create_window(100, 100, window=entry_widget2)
    entry.trace(«w», lambda *args: character_limit1(entry))
    new_window1.mainloop()

def character_limit(entry_text):
    if len(entry_text.get()) > 0:
        entry_text.set(entry_text.get()[-1])

root = Tk() # окно
root.title("(Текст1)") # название окна
root.geometry('830x880') # Размер окна
frame1 = Frame(root, bg='white')
frame1.place(relx=0.0, rely=0.0, relwidth=1.0, relheight=1.0)
canvas2 = Canvas(frame1, width=800, height=230, bg='white')
canvas2.pack()
entry_text = StringVar() # the text in your entry
entry_widget = Entry(canvas2, width=20, textvariable=entry_text) # the entry
canvas2.create_window(100, 100, window=entry_widget)
entry_text.trace(«w», lambda *args: character_limit(entry))
re()
root.mainloop()
12.04.23

Глеб Черняк

Читать ответы

Олег Николаевич

Читать ответы

Alexander

Читать ответы
Посмотреть всех экспертов из раздела Технологии
Пользуйтесь нашим приложением Доступно на Google Play Загрузите в App Store