我正在编写一个python脚本,可以显示我选择标签的按钮。
该程序包含4个radiobutton和1个标签。
到目前为止我的程序问题是我的程序无法重复。此外,我需要在标签周围设置边框。
我正在使用python 2.7而且我无法发布图片,因为我没有足够的声誉
= Label(the_window, text = 'Null', fg = 'black',font = ('Times', 36), width = 8)
button.grid(row=2,column=1)
button.grid(row=3,column=1)
button.grid(row=2,column=2)
button.grid(row=3,column=2)
这是我的设置
答案 0 :(得分:1)
您可以通过指定relief
:
direction_status = Label(the_window, text = 'Null', fg = 'black',
font = ('Times', 36), width = 8, relief=GROOVE)
使用单个IntVar,而不是将四个BooleanVars用作单选按钮的value
属性。这将向Tkinter表明单选按钮属于同一组,并确保一次只能选择一个。
from Tkinter import *
# Create a window
the_window = Tk()
# Give the window a title
the_window.title('Compass')
##Label widget to display initial Compass's status
direction_status = Label(the_window, text = 'Null', fg = 'black',
font = ('Times', 36), width = 8)
## Function that define the label's text when radiobuttion is being selected
def update_the_window():
if v.get() == 1:
direction_status['text'] = 'NW'
if v.get() == 2:
direction_status['text'] = 'SW'
if v.get() == 3:
direction_status['text'] = 'NE'
if v.get() == 4:
direction_status['text'] = 'SE'
## Label Frame for direction_status
direction_status_frame = LabelFrame(the_window, relief = 'groove',
borderwidth = 2)
v = IntVar()
## 4 Buttons that change the status sorted by directions
NW_button = Radiobutton(text = 'North-West', variable = v,
value= 1, command=update_the_window, padx=20)
NW_button.pack(anchor=W)
SW_button = Radiobutton(text = 'South-West', variable = v,
value= 2, command = update_the_window, padx=20)
SW_button.pack(anchor=W)
NE_button = Radiobutton(text= 'North-East', variable = v,
value= 3, command=update_the_window, padx=20)
NE_button.pack(anchor=W)
SE_button = Radiobutton(text= 'South-East', variable = v,
value= 4, command=update_the_window, padx=20)
SE_button.pack(anchor=W)
## Grid geometry to put 4 radio buttons into the GUI
NW_button.grid(row=2,column=1)
SW_button.grid(row=3,column=1)
NE_button.grid(row=2,column=2)
SE_button.grid(row=3,column=2)
## Grid geometry manager to put the widget into the root window
margin = 5 ##pixels
direction_status.grid(padx=margin, pady=margin, row=1, column=1, columnspan=2)
#--------------------------------------------------------------------#
# Start the event loop to react to user inputs
the_window.mainloop()
您也可以使用StringVar,这会在一定程度上减少update_the_window
的大小。
from Tkinter import *
# Create a window
the_window = Tk()
# Give the window a title
the_window.title('Compass')
# PUT YOUR CODE HERE-------------------------------------------------#
##Label widget to display initial Compass's status
direction_status = Label(the_window, text = 'Null', fg = 'black',
font = ('Times', 36), width = 8)
## Function that define the label's text when radiobuttion is being selected
def update_the_window():
direction_status['text'] = direction.get()
direction = StringVar()
direction.set("NW")
update_the_window()
## Label Frame for direction_status
direction_status_frame = LabelFrame(the_window, relief = 'groove',
borderwidth = 2)
## 4 Buttons that change the status sorted by directions
NW_button = Radiobutton(text = 'North-West', variable = direction,
value= "NW", command=update_the_window, padx=20)
NW_button.pack(anchor=W)
SW_button = Radiobutton(text = 'South-West', variable = direction,
value= "SW", command = update_the_window, padx=20)
SW_button.pack(anchor=W)
NE_button = Radiobutton(text= 'North-East', variable = direction,
value= "NE", command=update_the_window, padx=20)
NE_button.pack(anchor=W)
SE_button = Radiobutton(text= 'South-East', variable = direction,
value= "SE", command=update_the_window, padx=20)
SE_button.pack(anchor=W)
## Grid geometry to put 4 radio buttons into the GUI
NW_button.grid(row=2,column=1)
SW_button.grid(row=3,column=1)
NE_button.grid(row=2,column=2)
SE_button.grid(row=3,column=2)
## Grid geometry manager to put the widget into the root window
margin = 5 ##pixels
direction_status.grid(padx=margin, pady=margin, row=1, column=1, columnspan=2)
#--------------------------------------------------------------------#
# Start the event loop to react to user inputs
the_window.mainloop()