0

I've created a simple GUI application with Tkinter.

I have two questions about this code:

import tkinter as tk
from tkinter import ttk, Grid, Frame, N, S, W, E, StringVar, Label, Entry, RAISED, Button, Checkbutton, Scrollbar

class mainApp(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.scan_button = Button(self.parent, text="Scan", command=self.scan_wifi)
        self.forget_button = Button(self.parent, text="Forget", command=self.forget_wifi)
        self.reboot_button = Button(self.parent, text="Reboot", command=self.reboot)
        frame=Frame(self.parent)
        Grid.rowconfigure(self.parent, 0, weight=1)
        Grid.columnconfigure(self.parent, 0, weight=1)
        frame.grid(row=0, column=0, sticky=N+S+E+W)
        grid=Frame(self.parent)
        grid.grid(sticky=N+S+E+W, column=0, row=7, columnspan=2)
        Grid.rowconfigure(self.parent, 7, weight=1)
        Grid.columnconfigure(self.parent, 0, weight=1)

        headings=('Name', 'Address', 'Quality', 'Channel', 'Signal Level', 'Encryption')
        row=[]
        rows=[]

        self.table = ttk.Treeview(show="headings", selectmode="browse")
        self.table["columns"]=headings
        self.table["displaycolumns"]=headings

        for head in headings:
            self.table.heading(head, text=head, anchor=tk.CENTER)
            self.table.column(head, width=30, anchor=tk.CENTER)

        self.scrolltable = tk.Scrollbar(command=self.table.yview)
        self.table.configure(yscrollcommand=self.scrolltable.set)
        self.scrolltable.grid(row=1, column=100, sticky=N+S)

        for row in rows:
            self.table.insert('', tk.END, values=tuple(row))

        self.table.bind('<ButtonRelease-1>', self.OnRelease)
        self.table.grid(row=0, rowspan=14, columnspan = 21, sticky=N+S+E+W)
        self.scan_button.grid(row=15, column = 1, columnspan = 1,   sticky=N+S+E+W)
        self.forget_button.grid(row=15, column = 0, columnspan = 1 , sticky=N+S+E+W)
        self.reboot_button.grid(row=15, column = 3, columnspan = 1 , sticky=N+S+E+W)

    def OnRelease(self, event):
        pass

    def scan_wifi(self):
        pass

    def forget_wifi(self):
        pass

    def reboot(self):
        pass

root=tk.Tk()

app = mainApp(root)
root.mainloop()

1) How I can move the button on the top of the window?

2) Why, if I resize the window, the button "Forget" becomes bigger than other buttons? How I can make all buttons identical size?

2
  • For question 2, it is because you have called Grid.columnconfigure(self.parent, 0, weight=1). For question 1, do you mean that you want to move the bottom buttons to the top of the window? Commented Feb 13, 2020 at 4:48
  • Yes , top of the window, above Treeview. Commented Feb 13, 2020 at 5:00

1 Answer 1

1

Because you have called columnconfigure(0, weight=1) only, therefore only the Forget button will be resized when the window is resized.

To move the buttons to the top of the window, you need to rearrange the buttons to row=0 and the Treeview to row=1. Below is modified code based on yours:

import tkinter as tk
from tkinter import ttk

class mainApp(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        # buttons
        self.forget_button = tk.Button(self.parent, text='Forget', command=self.forget_wifi)
        self.scan_button = tk.Button(self.parent, text='Scan', command=self.scan_wifi)
        self.reboot_button = tk.Button(self.parent, text='Reboot', command=self.reboot)

        self.forget_button.grid(row=0, column=0, sticky='nsew')
        self.scan_button.grid(row=0, column=1, sticky='nsew')
        self.reboot_button.grid(row=0, column=2, sticky='nsew')

        # make the 3 buttons same width
        for i in range(3):
          self.parent.columnconfigure(i, weight=1)
        # make the treeview and the scrollbar to fill the remaining space
        self.parent.rowconfigure(1, weight=1)

        # treeview and scrollbar
        frame = tk.Frame(self.parent)
        frame.grid(row=1, column=0, columnspan=3, sticky='nsew')

        headings=('Name', 'Address', 'Quality', 'Channel', 'Signal Level', 'Encryption')
        self.table = ttk.Treeview(frame, show='headings', selectmode='browse')
        self.table['columns'] = headings
        self.table['displaycolumns'] = headings

        for head in headings:
            self.table.heading(head, text=head, anchor=tk.CENTER)
            self.table.column(head, width=30, anchor=tk.CENTER)

        self.scrolltable = tk.Scrollbar(frame, command=self.table.yview)
        self.table.configure(yscrollcommand=self.scrolltable.set)

        self.table.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
        self.scrolltable.pack(side=tk.RIGHT, fill=tk.Y)

        self.table.bind('<ButtonRelease-1>', self.OnRelease)

    def OnRelease(self, event):
        pass

    def scan_wifi(self):
        pass

    def forget_wifi(self):
        pass

    def reboot(self):
        pass

root=tk.Tk()
app = mainApp(root)
root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.