我开始使用Python中的TK构建我的程序的图形界面。我无法解决两个问题,分别是(1)按钮在窗口中的位置和(2)在函数中使用单选按钮的值。
这是我当前的代码:
这是我当前的代码:
root = tk.Tk()
root.title("START")
root.geometry("500x200+500+200")
v = tk.IntVar()
v.set(0) # initializing the choice
my_choise = [
("Basic",1),
("Advanced",2),
('Extreme',3)
]
def ShowChoice():
print(v.get())
tk.Label(root,
text="""Choose your configuration:""",
justify = tk.LEFT,
padx = 20).pack()
val = 0
for val, choise in enumerate(my_choise):
tk.Radiobutton(root,text=choise,padx = 20,variable=v,command=ShowChoice,value=val).pack(anchor=tk.W)
def star_program(value):
os.system("ifconfig")
def open_comments_file():
os.system("gedit /home/user/Desktop/comments.txt")
def open_links_file():
os.system("gedit /home/user/Desktop/links.txt")
frame = tk.Frame(root)
frame.pack()
open_file_c = tk.Button(frame,
text="Comments",
command=open_comments_file)
open_file_f = tk.Button(frame,
text="Links",
command=open_links_file)
button = tk.Button(frame,
text="Start",
command=star_program(v.get()))
button.pack(side=tk.LEFT)
open_file_f.pack(side=tk.LEFT)
open_file_c.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="Cancel",
command=quit)
slogan.pack(side=tk.LEFT)
root.mainloop()
我希望“链接”和“评论”按钮位于单选钮下方,一个在另一个下面。现在,所有的按钮都在同一行,但我希望将“开始”和“取消”放在窗口底部。
然后,我尝试在start_program函数中使用单选钮的值(choice),但未成功。我的想法是,根据所选单选钮的选择,在单击“开始”按钮时执行不同的操作:
def star_program(value):
if value == 0:
os.system("ifconfig")
else:
print "Goodbye"
此外,关于“开始”按钮,我遇到了一个奇怪的问题。即使我没有点击“开始”,程序也会运行“ifconfig”命令。而如果我点击“开始”,它则不执行任何操作。
有什么建议吗? 谢谢!!!