Search code examples
pythontkinterabstract-class

How to Sticky buttons in Tkinter to left of a Frame


So I'm creating a GUI application for the first time in Python, using TKinter and I'm running into some issues. First off, I've tried stickying buttons and looking at various solutions across stack Overflow on how to get them to stick to the left side of a frame. But I can't get my menu buttons to stick for some reason. I have a feeling it's to do with how I've laid out my columns, but I've tried column spanning and such, doesn't seem to fix it. The code is below (the buttons are in the SettingsGUI class) and the current client view is below that:

import tkinter as tk
from tkinter import ttk
from abc import ABC, abstractmethod

root = tk.Tk() 
root.title('GUI') 

class MUDGUI:
    def __init__(self, master):
        menuBar = SettingsGUI(self, master)
        roomChat = RoomChatGUI(self, master)

#Abstract class that all the chat boxes will inherit from
class ChatGUI(ABC):
    @abstractmethod
    def sendMessage(self):
        print("Send message to server and clear the entry box")

class RoomChatGUI(ChatGUI):
    def sendMessage(self):
        super().sendMessage()

    def __init__(self, parent, master):
        self.roomChatFrame = tk.Frame(master)
        self.roomChatFrame.grid(row=1)

        #Add text display that looks like command line
        self.roomChatText = tk.Text(self.roomChatFrame, height=10, width=75, bg="black", fg="white")
        self.roomChatText.grid(row=0)
        self.roomChatText.insert(tk.END, "Room chat frame")
        self.roomChatText.config(state=tk.DISABLED)

        #Add input (entry) 
        self.roomChatEntry = tk.Entry(self.roomChatFrame, width=100, bg="black", fg="white")
        self.roomChatEntry.bind('<Return>', sendMessage)
        self.roomChatEntry.grid(row=1)

class SettingsGUI:
    def __init__(self, parent, master):
        self.settingsFrame = tk.Frame(master)
        self.settingsFrame.grid(row=0, column=0)

        #Need settings menu
        self.settingsButton = tk.Button(self.settingsFrame, text="Settings")
        self.settingsButton.grid(row=0, column=0, sticky="W")

        #Need close button
        self.closeButton = tk.Button(self.settingsFrame, text="Close", command=self.settingsFrame.quit)
        self.closeButton.grid(row=0, column=1, sticky="W")

gui = MUDGUI(root)
root.mainloop()

GUI with weird button placements

The other issue I am having is with my entry box enter binding. In RoomChatGUI I am trying to bind the Enter key to the Entry box so that when the user presses 'Enter' the sendMessage method will be called, which should call its super sendMessage (defined in the abstract ChatGUI class). However, instead, it gives the error that "name 'sendMessage' is not defined". Not sure what to change here.

Any and all help would be appreciated


Solution

  • The buttons are sticking to the left side of the frame. The problem is that you haven't requested that the frame stick to the sides of the window.

    You need to define the sticky attribute when adding the frame to the window:

    self.settingsFrame.grid(row=0, column=0, sticky="nsew")