如何在GUI窗口的不同行中打印数组?

时间:2019-07-10 05:52:23

标签: python tkinter tkinter-canvas

我想在GUI的不同行中显示数组的数据。这是代码。

import tkinter as tk
window = tk.Tk()
window.configure(background='white')

ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
w = 200  # width for the Tk root
h = 500  # height for the Tk root
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)



window.geometry('%dx%d+%d+%d' % (w, h, x, y))
canvas = tk.Canvas(window, bg="white", width=980, height=580, highlightthickness=0)
canvas.pack()
canvas_scroll = tk.Scrollbar(canvas, command=canvas.yview)
canvas_scroll.place(relx=1, rely=0, relheight=1, anchor=tk.NE)
canvas.configure(yscrollcommand=canvas_scroll.set, scrollregion=())


op = ("Hello", "Good Morning", "Good Evening", "Good Night", "Bye")

l9 = tk.Label(canvas, text=op, font= "calibri 13", bg="white")
canvas.create_window(33,33, window=l9, anchor=tk.NW)  



window.mainloop()

我想要类似的输出:

你好

早上好

晚上好

晚安

再见

1 个答案:

答案 0 :(得分:1)

您也可以尝试以下方法:

op = ("Hello", "Good Morning", "Good Evening", "Good Night", "Bye")

def applytoLabel():
    n = len(op)
    element = ''
    for i in range(n):
        element = element + op[i]+'\n' 
    return element

l9 = tk.Label(canvas, text=applytoLabel(), font= "calibri 13", bg="white")
canvas.create_window(33,33, window=l9, anchor=tk.NW)

输出:

enter image description here

完整代码:

import tkinter as tk
window = tk.Tk()
window.configure(background='white')

ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
w = 200  # width for the Tk root
h = 500  # height for the Tk root
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)



window.geometry('%dx%d+%d+%d' % (w, h, x, y))
canvas = tk.Canvas(window, bg="white", width=980, height=580, highlightthickness=0)
canvas.pack()
canvas_scroll = tk.Scrollbar(canvas, command=canvas.yview)
canvas_scroll.place(relx=1, rely=0, relheight=1, anchor=tk.NE)
canvas.configure(yscrollcommand=canvas_scroll.set, scrollregion=())


op = ("Hello", "Good Morning", "Good Evening", "Good Night", "Bye")

def applytoLabel():
    n = len(op)
    element = ''
    for i in range(n):
        element = element + op[i]+'\n' 
    return element

l9 = tk.Label(canvas, text=applytoLabel(), font= "calibri 13", bg="white")
canvas.create_window(33,33, window=l9, anchor=tk.NW) 

window.mainloop()