Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a margin to a tkinter window

Tags:

python

tkinter

So I have so far a simple python tkinter window and i'm adding text, buttons, etc.

snippet:

class Cfrm(Frame):

    def createWidgets(self):

        self.text = Text(self, width=50, height=10)
        self.text.insert('1.0', 'some text will be here')
        self.text.tag_configure('big', font=('Verdana', 24, 'bold'))


        self.text["state"] = "disabled"
        self.text.grid(row=0, column=1)

        self.quitw = Button(self)
        self.quitw["text"] = "exit",
        self.quitw["command"] = self.quit
        self.quitw.grid(row=1, column=1)


    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

the problem is, I want to have about a 15-20 px margin around the window, I looked everywhere, and I couldn't find a solution. Also

self.text.tag_configure('big', font=('Verdana', 24, 'bold'))

doesn't work. Any possible solutions?

like image 633
Jack S. Avatar asked Sep 04 '10 17:09

Jack S.


People also ask

How do I change frame size in tkinter?

The frame widget in Tkinter works like a container where we can place widgets and all the other GUI components. To change the frame width dynamically, we can use the configure() method and define the width property in it.

Does tkinter have Rowspan?

tkinter Tkinter Geometry Managers grid()It uses column , columnspan , ipadx , ipady , padx , pady , row , rowspan and sticky .

What is padding in tkinter?

Padding enhances the layout of the widgets in an application. While developing an application in Tkinter, you can set the padding in two or more ways. The geometry manager in Tkinter allows you to define padding (padx and pady) for every widget (label, text, button, etc).


1 Answers

You can add an innerFrame with some borderwidth or ipadx,ipady attributes to the root Tk() object. Then put everything in it. And to be sure that innerFrame's width and height values are as the window's, use fill and expand attributes.

root = tk.Tk()
innerFrame = tk.Frame(root, borderwidth=25, bg="red")
innerFrame.pack(fill="both", expand=True)

someButton = tk.Button(innerFrame, text="Button1")
someButton.pack()

or

root = tk.Tk()
innerFrame = tk.Frame(root, bg="red")
innerFrame.pack(ipadx=15, ipady=15, fill="both", expand=True)

someButton = tk.Button(innerFrame, text="Button1")
someButton.pack()    
like image 80
Oguz Avatar answered Oct 14 '22 14:10

Oguz