【问题标题】:How do I print an array in different lines in GUI window? [duplicate]如何在 GUI 窗口的不同行中打印数组? [复制]
【发布时间】:2019-07-10 05:52:23
【问题描述】:

我想在 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()

我想要这样的输出:

你好

早安

晚上好

晚安

再见

【问题讨论】:

    标签: python tkinter tkinter-canvas


    【解决方案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)
    

    输出:

    完整代码:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-25
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 2022-01-10
      • 2012-12-25
      相关资源
      最近更新 更多