Skip to main content
 首页 » 编程设计

python之修复 Tkinter 中 RadioButtons 的设计

2024年12月31日8kevingrace

我想将我的单选按钮与标题链接起来,例如将我的标题与单选按钮合并。这是我实现的代码:

but1 = Radiobutton(text = "Current",value = 0) 
but1.place(x =400,y = 150) 
but2 = Radiobutton(text = "Previous",value = 1) 
but2.place(x =320,y = 150) 
but3 = Radiobutton(text = "Current",value = 2) 
but3.place(x =400,y = 260) 
but4 = Radiobutton(text = "Previous",value = 3) 
but4.place(x =320,y = 260) 
the_window.geometry("510x430") 
 
label1 = Label(the_window,text = "Most-Discussed \n TV SHOW", font =  
"Times 10 bold") 
label1.place(x = 350,y = 110) 
 
label2 = Label(the_window,text = "Most-Discussed \n TV SHOW", font =  
"Times 10 bold") 
label2.place(x = 350,y = 230) 

这是实际结果:

这是预期的结果:

请您参考如下方法:

我不知道你是否知道这个,但那个小部件叫做 LabelFrame .请参见下面的示例。

附言我已将您的几何管理器更改为 grid

import tkinter as tk 
 
root = tk.Tk() 
 
f1 = tk.LabelFrame(root, text="Most-Discussed \n TV SHOW", labelanchor="n", font="Verdana 12 bold", bd=4) 
f1.grid(row=0, column=0, padx=10, pady=10) 
 
f2 = tk.LabelFrame(root, text="Music Vinyl \n Album Chart", labelanchor="n", font="Verdana 12 bold", bd=4) 
f2.grid(row=1, column=0, padx=10, pady=10) 
 
but1 = tk.Radiobutton(f1, text="Current", value = 0) 
but1.grid(row=0, column=0) 
but2 = tk.Radiobutton(f1, text="Previous" ,value = 1) 
but2.grid(row=0, column=1) 
but3 = tk.Radiobutton(f2, text="Current", value = 2) 
but3.grid(row=0, column=0) 
but4 = tk.Radiobutton(f2, text="Previous" ,value = 3) 
but4.grid(row=0, column=1) 
 
root.mainloop()