我想在标签上创建框架,并尝试了很多代码,但没有成功。还尝试使屏幕左侧的检查按钮没有边框。谁能帮我?谢谢
我到目前为止
但是我想让它看起来像这样
show_status = Label(dashboard, bd = 5, text = 'Even', fg = 'black',
font = ('Arial', 70), width = 8)
def update_dashboard():
three_buttons = Label(dashboard, relief = 'groove')
Alpha_button = Checkbutton(three_buttons, text = 'Alpha',
variable = alpa_1,
command = update_dashboard)
Beta_button = Checkbutton(three_buttons, text = 'Beta',
variable = beta_2,
command = update_dashboard)
Gamma_button = Checkbutton(three_buttons, text = 'Gamma',
variable = gemma_3,
command = update_dashboard)
Alpha_button.grid(row = 1, column = 0, sticky = 'w')
Beta_button.grid(row = 1, column = 2, sticky = 'w')
Gamma_button.grid(row = 1, column = 4, sticky = 'w')
margin = 5 # pixels
show_status.grid(padx = margin, pady = margin, row = 1,
column = 1, columnspan = 2,)
three_buttons.grid(row = 4, column = 2, sticky = W)
dashboard.mainloop()
最佳答案
您可以使用框架或画布,并在其上绘制其余的小部件。让我们依靠grid layout manager使用框架。
要获得所需的效果,您只需使用选项columnspan
选项将标签覆盖在检查按钮小部件的3列上。
完整程序
这是使用面向对象概念的简单解决方案:
'''
Created on May 8, 2016
@author: Billal Begueradj
'''
import Tkinter as Tk
class Begueradj(Tk.Frame):
'''
Dislay a Label spanning over 3 columns of checkbuttons
'''
def __init__(self, parent):
'''Inititialize the GUI
'''
Tk.Frame.__init__(self, parent)
self.parent=parent
self.initialize_user_interface()
def initialize_user_interface(self):
"""Draw the GUI
"""
self.parent.title("Billal BEGUERADJ")
self.parent.grid_rowconfigure(0,weight=1)
self.parent.grid_columnconfigure(0,weight=1)
self.parent.config(background="lavender")
# Create a Frame on which other elements will be attached to
self.frame = Tk.Frame(self.parent, width = 500, height = 207)
self.frame.pack(fill=Tk.X, padx=5, pady=5)
# Create the checkbuttons and position them on the second row of the grid
self.alpha_button = Tk.Checkbutton(self.frame, text = 'Alpha', font = ('Arial', 20))
self.alpha_button.grid(row = 1, column = 0)
self.beta_button = Tk.Checkbutton(self.frame, text = 'Beta', font = ('Arial', 20))
self.beta_button.grid(row = 1, column = 1)
self.gamma_button = Tk.Checkbutton(self.frame, text = 'Gamma', font = ('Arial', 20))
self.gamma_button.grid(row = 1, column = 2)
# Create the Label widget on the first row of the grid and span it over the 3 checbbuttons above
self.label = Tk.Label(self.frame, text = 'Even', bd = 5, fg = 'black', font = ('Arial', 70), width = 8, relief = 'groove')
self.label.grid(row = 0, columnspan = 3)
# Main method
def main():
root=Tk.Tk()
d=Begueradj(root)
root.mainloop()
# Main program
if __name__=="__main__":
main()
演示版
这是正在运行的程序的屏幕截图:
关于python - 在python的标签上添加框架,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37098040/