几天以来我一直在努力学习python和tkinter。 我想在框架中创建动态复选框(来自文件的值)。你知道吗
这是我的密码:
import tkinter as tk
lst="lst.txt"
class DisplayApp:
def __init__(self):
self.root = tk.Tk()
self.root.title("My Menu")
self.build_gui()
def build_gui(self):
frame_1 = tk.LabelFrame(self.root, text="Frame 1")
frame_1.grid(row=2, columnspan=3, sticky='WE',
padx=5, pady=5, ipadx=5, ipady=5)
linestring = open(lst, 'r').read()
for checkBoxName in linestring:
c = tk.Checkbutton(frame_1, text=checkBoxName,)
c.pack(side=tk.LEFT)
def clicked(self):
pass
def main(self):
self.root.mainloop()
if __name__ == "__main__":
app = DisplayApp()
app.main()
这是文件lst.txt文件地址:
laptopname_1
laptopname_2
laptopname_3
laptopname_n
...
这将导致错误:
[root@hdid_master ~]# ./test_menu_2.py
File "./test_menu_2.py", line 35
for checkBoxName in linestring:
^
TabError: inconsistent use of tabs and spaces in indentation
非常感谢你的帮助。 致以最诚挚的问候。你知道吗
您的代码错误告诉您混用了
tab
和space
您不应该这样做。Python依赖于一致的缩进来知道一行代码的结束和另一行代码的开始。请确保遵循PEP8指南,每个缩进级别使用4个空格。你知道吗因此,您需要对按钮创建进行一些更改。现在它将为每行的每个字母创建一个不同的按钮。你知道吗
请参阅下面更正的示例,如果您有任何问题,请告诉我:
相关问题 更多 >
编程相关推荐