![](/img/trans.png)
[英]how to make this function work in order with the tkinter label
[英]How to make function or loop for making label in tkinter
由於每個 label 都有自己的文本,因此使用循環沒有多大幫助。
建議使用 function 創建 label 並將其放入 canvas 中。 它將減少一些冗余代碼:
import tkinter as tk
def new_label(canvas, x, y, **kw):
lbl = tk.Label(canvas, **kw)
item_id = canvas.create_window(x, y, window=lbl)
# return the label reference and the item ID in case they are required later
return lbl, item_id
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack()
new_label(canvas, 250, 30, text="Welcome", font="Arial 16 bold", width=20, fg="white", bg="blue")
new_label(canvas, 70, 70, text="Type Value 1")
new_label(canvas, 70, 110, text="Type Value 2")
new_label(canvas, 130, 150, text="Choose the number of operation")
new_label(canvas, 70, 175, text="1. Add")
new_label(canvas, 150, 175, text="2. Substract")
new_label(canvas, 255, 175, text="3. Multiplication")
new_label(canvas, 355, 175, text="4. Division")
new_label(canvas, 270, 250, text="Result:")
root.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.