简体   繁体   English

Python,Tkinter列表框

[英]Python, tkinter listbox

I have a problem with the listbox because it does not display names like in the first listbox under 1T, only on one line, I want to display the names one under the other. 我的列表框有问题,因为它不会像在1T下的第一个列表框中那样显示名称,仅在一行上显示,而我想在另一个下显示一个名称。 I have no idea how to do this. 我不知道该怎么做。 Thank you for every advice and attention to the code below 感谢您对以下代码的建议和关注

在此处输入图片说明

import random
import tkinter, sys
from tkinter import *
import tkinter.messagebox as messagebox

los = []
list = ['1. Kamil Winnicki', '#2. Wiktor Jasiński', '3. Adam Turowski', '#4. Arek Major', '5. Dominik Piechotka', '#6. Jakub Laskowski', '7. Jakub Materak', '8. Kacper Kołodziejski',
        '#9. Kamil Stankiewicz', '10. Konrad Nosek', '11. Krzysiek Wawszczak', '12. Andrzej Oleksiak', '13. Miłosz Tarucin', '14. Paweł Pawłowski', '#15. Mateusz Lebioda']
lines = list

for line in lines:
    if line [0] != '#':
        los.append(line)

main = tkinter.Tk()

def koniec():
    main.destroy()

def losowanie():
    messagebox.showinfo(message=random.sample(los ,1))

#nagłowek
te = tkinter.Label(main, text = 'Lista 1T:')
te.pack()

#Wyswietla liste 1T
listbox = Listbox(main, width=21, height=15)
listbox.insert(1, '1. Mateusz Lebioda', '2. Jakub Laskowski', '3. Kamil Winnicki', '4. Wiktor Jasiński', '5. Adam Turowski', '6. Arek Major', '7. Dominik Piechotka', '8. Jakub Materak', '9. Kacper Kołodziejski', '10. Kamil Stankiewicz', '11. Konrad Nosek', '12. Krzysiek Wawszczak', '13. Andrzej Oleksiak', '14. Miłosz Tarucin', '15. Paweł Pawłowski')
listbox.pack()

#Obecne osoby
obecne1 = tkinter.Label(main, text = 'Obecne osoby:')
obecne1.pack()

obecne = Listbox(main)
obecne.insert(1, los)
obecne.pack()



#losuje
y = tkinter.Button(main, text = 'losuj', command = losowanie)
y.pack()


#wyjscie z aplikacji
x = tkinter.Button(main, text = 'Zakoncz', command = koniec)
x.pack()

main.mainloop()

You need to unpack your list when inserting. 插入时,您需要打开列表的包装。

Changing insertion line would be enough. 更改插入线就足够了。

obecne.insert("end", *los)
                    #^ notice this star here. That one makes the unpacking

or you can just iterate over your list items using for loop. 或者您也可以使用for循环遍历列表项。

obecne = Listbox(main)
for item in los:
    obecne.insert("end", item)
obecne.pack()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM