Как вывести значения из множеста или списка в текстовое поле
Имеется окно с жанрами, при нажатии на жанр в окне он сохраняется в set(). Как вывести сохраненные в set() данные в текстовое поле. Т.е хочу после выбора и подтверждения, выбранные жанры выходили в виде текста. Вместо set можно использовать и list это не принципиально как вам удобнее. Главное понять как выводить данные (из list или set) на текстовое поле на окне.
from tkinter import *
copy=set()
def window_with_janre():
jenre_window = Tk()
jenre_window.title("Выбор жанара.")
jenre_window.geometry("650x400")
jenre_window.resizable(width=FALSE, height=FALSE)
button_janr1 = Button(jenre_window, text='Action and adventure', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Action and adventure"))
button_janr1.place(x=20, y=20)
button_janr2 = Button(jenre_window, text='Detective', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Detective"))
button_janr2.place(x=20, y=60)
button_janr3 = Button(jenre_window, text='Sci-fi', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Sci-fi"))
button_janr3.place(x=20, y=100)
button_janr4 = Button(jenre_window, text='Historical fiction', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Historical fiction"))
button_janr4.place(x=20, y=140)
button_janr5 = Button(jenre_window, text='Dystopia', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Dystopia"))
button_janr5.place(x=20, y=180)
button_janr6 = Button(jenre_window, text='Fantasy', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Fantasy"))
button_janr6.place(x=20, y=220)
button_janr7 = Button(jenre_window, text='Romance novel', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Romance novel"))
button_janr7.place(x=20, y=260)
button_janr8 = Button(jenre_window, text='Short stories', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Short stories"))
button_janr8.place(x=20, y=300)
button_janr9 = Button(jenre_window, text='Western', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Western"))
button_janr9.place(x=20, y=340)
button_janr10 = Button(jenre_window, text='Horror', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Horror"))
button_janr10.place(x=250, y=20)
button_janr11 = Button(jenre_window, text='Classic', bg='gold', font='Arial 13', height=1, command=lambda: copy.add("Classic"))
button_janr11.place(x=250, y=60)
jenre_window.mainloop()
window_with_janre()
Ответы (1 шт):
Автор решения: versetty777
→ Ссылка
Тут лучше применить insert() у объекта Text. Например, создадим Text и присвоим его переменной text_field:
text_field = Text(jenre_window, width=40, height=10)
text_field.place(x=200, y=20)
Потом при нажатии на кнопку с жанром, можно добавлять выбр. жанр в text_field с помощью метода insert():
button_janr1 = Button(jenre_window, text='Action and adventure', bg='gold', font='Arial 13', height=1, command=lambda: text_field.insert(END, "Action and adventure" + "\n"))
Еще решение:
from tkinter import *
def window_with_janre():
jenre_window = Tk()
jenre_window.title("Выбор жанара.")
jenre_window.geometry("650x400")
jenre_window.resizable(width=FALSE, height=FALSE)
selected_genres = ""
def update_text_field():
text_field.config(state=NORMAL)
text_field.delete(1.0, END)
text_field.insert(END, selected_genres)
text_field.config(state=DISABLED)
text_field = Text(jenre_window, width=40, height=10)
text_field.place(x=200, y=20)
text_field.config(state=DISABLED)
button_janr1 = Button(jenre_window, text='Action and adventure', bg='gold', font='Arial 13', height=1, command=lambda: selected_genres += "Action and adventure" + ", " if selected_genres else "Action and adventure" + ", ",
command=update_text_field)
button_janr1.place(x=20, y=20)
button_janr2 = Button(jenre_window, text='Detective', bg='gold', font='Arial 13', height=1, command=lambda: selected_genres += "Detective" + ", " if selected_genres else "Detective" + ", ",
command=update_text_field)
button_janr2.place(x=20, y=60)
# and so on
или так:
from tkinter import *
def window_with_janre():
jenre_window = Tk()
jenre_window.title("Выбор жанара.")
jenre_window.geometry("650x400")
jenre_window.resizable(width=FALSE, height=FALSE)
selected_genres = []
def update_text_field():
text_field.config(state=NORMAL)
text_field.delete(1.0, END)
for genre in selected_genres:
text_field.insert(END, genre + "\n")
text_field.config(state=DISABLED)
text_field = Text(jenre_window, width=40, height=10)
text_field.place(x=200, y=20)
text_field.config(state=DISABLED)
button_janr1 = Button(jenre_window, text='Action and adventure', bg='gold', font='Arial 13', height=1, command=lambda: selected_genres.append("Action and adventure"))
button_janr1.place(x=20, y=20)
button_janr2 = Button(jenre_window, text='Detective', bg='gold', font='Arial 13', height=1, command=lambda: selected_genres.append("Detective"))
button_janr2.place(x=20, y=60)
#and so on
confirm_button = Button(jenre_window, text='Подтвердить', command=update_text_field)
confirm_button.place(x=20, y=300)
Следующий код тоже может помочь:
from tkinter import *
def window_with_genres():
genre_window = Tk()
genre_window.title("Select Genres")
genre_window.geometry("650x400")
genre_window.resizable(width=FALSE, height=FALSE)
selected_genres = ""
def update_label():
genre_label.config(text=selected_genres)
genre_label = Label(genre_window, text="")
genre_label.place(x=200, y=20)
action_adventure_button = Button(genre_window, text="Action and Adventure", command=lambda: selected_genres.append("Action and Adventure"))
action_adventure_button.place(x=20, y=20)
detective_button = Button(genre_window, text="Detective", command=lambda: selected_genres.append("Detective"))
detective_button.place(x=20, y=60)
#and so on
confirm_button = Button(genre_window, text='Confirm', command=update_label)
confirm_button.place(x=20, y=300)