我在使用tkinter的create_text时遇到了一些麻烦。我正在尝试遍历列表并让create_text逐个输出列表中的每个项目。我无法理解这一点,因为每次我尝试过,它都不会像我想要的那样工作。这里有一些代码可以解释这个问题:
class GUI(Frame):
def __init__(self, master):
self.test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
self.c = Canvas(master, width=300, height=300)
self.c.pack()
for items in self.test_list:
items = items
for i in range(0, 300, 100):
for j in range(0, 300, 100):
self.c.create_text(j + 25, i + 20, text=items)
root = Tk()
root.title("Test")
root.geometry("300x300")
GUI(root)
mainloop()
谢谢,我感谢您的帮助。
答案 0 :(得分:2)
您的代码存在严重的缩进问题
此外,您没有在任何对象上调用Range("A1").NumberFormat = "General"
然后,画布上对象的位置在可见窗口之外:
我修复了代码,使其运行,并在画布上显示内容;从那里,您可以根据自己的需要进行修改。
mainloop
答案 1 :(得分:0)
这有两个外环。
# iterates, items == 9 now
for items in self.test_list:
items = items
# uses 9 over and over
for i in range(0, 300, 100):
for j in range(0, 300, 100):
self.c.create_text(j + 25, i + 20, text=items)
也许试试这个。
for items in self.test_list:
for i in range(0, 300, 100):
for j in range(0, 300, 100):
self.c.create_text(j + 25, i + 20, text=items)