Reputation: 23
Why the grid is not centered to the window size? What is the maximum row and column now of the grid? Also, why the buttons are not square since i define their height and width to be equal? this is my code:
from Tkinter import *
class Application:
def __init__(self, master=None):
self.master = master
self.createWidgets_0()
def createWidgets_0(self):
self.but_Next = Button(self.master, text="Next", height = 10, width = 10)
self.but_Next.grid(row=1, column=2)
self.but_fil1 = Button(self.master, text="Filler", height = 10, width = 10)
self.but_fil1.grid(row=1, column=1)
self.but_fil2 = Button(self.master, text="Filler", height = 10, width = 10)
self.but_fil2.grid(row=0, column=1)
self.but_fil3 = Button(self.master, text="Filler", height = 10, width = 10)
self.but_fil3.grid(row=2, column=1)
self.but_Close = Button(self.master, text="Close", command=self.master.quit, height = 10, width = 10)
self.but_Close.grid(row=1, column=0)
root = Tk()
app = Application(root)
app.master.geometry("800x600")
app.master.resizable(0, 0)
root.mainloop()
root.destroy()
this is the output i get of the above code
Upvotes: 2
Views: 1643
Reputation: 386352
You haven't told tkinter what to do if it has extra space in the window. You do that by giving rows or columns a weight, which tells tkinter relatively how much of the extra space should be given to that row or column. The default weight is zero, meaning that no row or column will be given any more space than it has requested.
As a rule of thumb, you should always give at least one row and one column a non-zero weight. Usually this will be the "hero" row or column (typically, one with a text widget, canvas, or other large widget). If you want all your widgets centered -- somewhat like a web page -- give want to row and column zero and also a row and column at least one larger than your last row and column. If you want all of your rows and columns to expand equally to fill the window, give every row and column that has a widget the same weight.
To give a row or column weight, use grid_rowconfigure
and grid_columnconfigure
. The first argument is a row or column number, or a tuple of numbers. For example, to give all of the extra space to rows zero and two, you would do this:
self.master.grid_rowconfigure((0,2), weight=1)
Upvotes: 2
Reputation: 15236
If you specify an image background of a 1x1 image you can resize your button without issues.
Keep in mind without an image being set to the button tkinter will interpret height and with based on text size. With an image it should interpret height and width in pixels.
import tkinter as tk
root = tk.Tk()
tk.Frame(root)
my_image = tk.PhotoImage(file="path_to_image/1x1.gif")
tk.Button(text="Button with 1x1 image", image=my_image, compound="left", height=100).grid(row=0, column=0)
root.mainloop()
Upvotes: 0