【问题标题】:Resize button in Tkinter GUI PythonTkinter GUI Python中的调整大小按钮
【发布时间】:2020-02-13 04:25:53
【问题描述】:

我用 Tkinter 创建了一个简单的 GUI 应用程序。

我对这段代码有两个问题:

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) 如何移动窗口顶部的按钮?

2) 为什么,如果我调整窗口大小,“忘记”按钮会变得比其他按钮大?如何使所有按钮的大小相同?

【问题讨论】:

  • 第二个问题,是因为你打了Grid.columnconfigure(self.parent, 0, weight=1)。对于问题1,您的意思是要将底部按钮移动到窗口顶部吗?
  • 是的,窗口顶部,Treeview 上方。

标签: python tkinter


【解决方案1】:

因为您只调用了columnconfigure(0, weight=1),因此在调整窗口大小时只会调整Forget 按钮的大小。

要将按钮移动到窗口顶部,您需要将按钮重新排列为row=0,将Treeview 重新排列为row=1。以下是根据您的修改代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2015-03-16
    • 2019-07-28
    • 1970-01-01
    相关资源
    最近更新 更多