Jack S.
Jack S.

Reputation: 2803

How to add a margin to a tkinter window

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?

Upvotes: 18

Views: 93960

Answers (4)

Oguz
Oguz

Reputation: 41

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()    

Upvotes: 4

Jack S.
Jack S.

Reputation: 2803

Ok, here is the solution I found for question 1:

self.grid(padx=20, pady=20)

Removing .text seems to change the whole frame. I still haven't solved problem 2.

Upvotes: 31

Bryan Oakley
Bryan Oakley

Reputation: 386362

Use the pad options (padx, pady, ipadx, ipady) for the grid command to add padding around the text widget. For example:

self.text.grid(row=0, column=1, padx=20, pady=20)

If you want padding around the whole GUI, add padding when you pack the application frame:

self.pack(padx=20, pady=20)

When you say the tag command doesn't work, how do you define "doesn't work"? Are you getting an error? Does the font look big but not bold, bold but not big, ...? The command looks fine to me, and when I run it it works fine.

Your example doesn't show that you're actually applying that tag to a range of text. Are you? If so, how? If you do the following, what happens?

self.text.insert("1.0", 'is this bold?', 'big')

Upvotes: 16

rectangletangle
rectangletangle

Reputation: 53051

A quick way to do it is adjust your relief style to flat, then you only have to adjust your border width.

self.Border = Tkinter.Frame(self, relief='flat', borderwidth=4)

Upvotes: 2

Related Questions