0

я делаю мессенджер на tkinter. Пытаюсь релизовать чтобы пользователь вводил ник и текст а потом отображалось как {name} - 'text', что у меня не правильно? текст ошибки:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: 'tuple' object is not callable
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: 'tuple' object is not callable
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: 'tuple' object is not callable
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: 'tuple' object is not callable
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: 'tuple' object is not callable

код указан снизу

from tkinter import *
root = Tk()
root.geometry('1920x1080')

background = Label(root, bg='#cfd5db', text='SPECTAPS MESSENGER').pack(fill='both', anchor=SW)
input_user = StringVar()
input_field = Entry(root, text=input_user, bg='lightblue')
input_field.pack(side=BOTTOM, anchor=E)

input_name1 = StringVar()
input_name2 = Entry(root, text=input_name1, bg='#09C')
input_name2.pack(side=TOP, anchor=NW)
global get_name
def name(event):
    get_name = input_name2.get()
    return 'break'

def enter_pressed(event):
    input_get = input_field.get()
    label = Label(frame, text=input_get, bg='#F5F5F5')
    input_user.set('')
    label.pack(anchor=W)
    return 'break'



frame = Text(root, width=57, height=47, bg='#F5F5F5')
frame.pack_propagate(False)
input_field.bind('<Return>', (name, enter_pressed))
frame.pack(anchor=E, side=RIGHT)
frame.config(state=DISABLED)
root.mainloop()

1 ответ 1

0

Вторым параметром bind метода должна передаваться функция (метод), а вы передаете кортеж (tuple), вот и ругается, что tuple не вызываемый объект (его нельзя вызывать как функцию).

В вашем случае нужно просто объединить две функции в одну, в этой функции получаете и имя пользователя из текстового поля, и выводимый текст, и добавляете их во фрейм:

...


def enter_pressed(event):
    name = input_name2.get()
    # Метка синего цвета с именем пользователя + двоеточие
    label = Label(frame, text=name + ":", fg='#0000FF', bg='#F5F5F5')  
    label.pack(anchor=W)

    input_get = input_field.get()
    label = Label(frame, text=input_get, bg='#F5F5F5')
    label.pack(anchor=W)

    input_user.set('')


frame = Text(root, width=57, height=47, bg='#F5F5F5')
frame.pack_propagate(False)
input_field.bind('<Return>', enter_pressed)
frame.pack(anchor=E, side=RIGHT)
frame.config(state=DISABLED)
root.mainloop()

Вообще, для добавления текста в текстовое поле лучше использовать метод insert, а не использовать текстовое поле как фрейм для меток:

...


def enter_pressed(event):
    text_field.config(state=NORMAL)

    name = input_name2.get()
    # Добавляем имя пользователя + двоеточие, для него указываем тег user_name
    text_field.insert(END, name + ":" + "\n", "user_name")
    
    input_get = input_field.get()
    text_field.insert(END, input_get + "\n")

    text_field.config(state=DISABLED)
    input_user.set('')


text_field = Text(root, width=57, height=47, bg='#F5F5F5')
text_field.pack_propagate(False)
input_field.bind('<Return>', enter_pressed)
text_field.pack(anchor=E, side=RIGHT)
text_field.config(state=DISABLED)

# Для текста с тегом "user_name" указываем синий цвет текста
text_field.tag_configure('user_name', foreground="blue")

root.mainloop()
1
  • Спасибо большое, уже не знал что делать
    – tiveriny
    10 янв 2022 в 14:03

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.