Adding padding to a tkinter widget only on one side

后端 未结 1 1843
情歌与酒
情歌与酒 2020-11-30 01:21

How can I add padding to a tkinter window, without tkinter centering the widget? I tried:

 self.canvas_l = Label(self.master, text=\"choose a color:\", font=         


        
相关标签:
1条回答
  • 2020-11-30 01:44

    The padding options padx and pady of the grid and pack methods can take a 2-tuple that represent the left/right and top/bottom padding.

    Here's an example:

    import tkinter as tk
    
    class MyApp():
        def __init__(self):
            self.root = tk.Tk()
            l1 = tk.Label(self.root, text="Hello")
            l2 = tk.Label(self.root, text="World")
            l1.grid(row=0, column=0, padx=(100, 10))
            l2.grid(row=1, column=0, padx=(10, 100)) 
    
    app = MyApp()
    app.root.mainloop()
    
    0 讨论(0)
提交回复
热议问题