Tkinter Image button taking up too much space

Viewed 42

So, my code is this:

import tkinter as tk
from tkinter import ttk
from tkinter import *
from PIL import Image, ImageTk

# Main root class
class MyUI:
    def __init__(self, parent):
        parent.geometry("310x310")
        parent.title("Sticky Card")

        # Frame
        frame = ttk.Frame(parent)

        frame.columnconfigure(0, weight=0)
        frame.columnconfigure(1, weight=2)
        frame.columnconfigure(2, weight=1)

        # Text
        text = tk.Text(frame)
        text.grid(row=1,column=0)

        # + (New Card)
        plus = Image.open("plussign.png")

        resized = plus.resize((30,30))

        img = ImageTk.PhotoImage(resized)
        label = ttk.Label(image = img)
        label.image = img

        plus = ttk.Button(frame, text="+", image=label.image, command = self.new_window)
        plus.grid(row=0, column=0, sticky='w')

        # Delete card button
        delete_card = ttk.Button(frame, text="Delete")
        delete_card.grid(row=0, column=0)

        # Frame grid
        frame.grid(row=0,column=0)

        # Tomorrow:
        # Make delete card button that deletes and exits that card exclusively, not exiting all cards at once like the premade "X" button

    # New window function (+)
    def new_window(self):
        win = Toplevel()
        MyUI(win)


# Running the program
root = Tk()
MyUI(root)
root.mainloop()

When I run it, the plus button takes up way too much space, and when I try setting another button in the same row, it's pushed far off the screen. I tried using sticky='w' and adding padx = (number) and it works the way I want to but I want to know why the plus button is taking up so much space and if there's a better, logical solution.

0 Answers
Related