无法在 tkinter 中调整按钮高度 python
Unable to resize button height in tkinter python
import tkinter
from tkinter import *
root = Tk()
root.title("Demo")
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.grid(sticky="ewns")
self.feet = StringVar()
self.meters = StringVar()
self.create_widgets()
def calculate(self):
try:
self.meters.set(int(0.3048 * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
def create_widgets(self):
self.master.resizable(width=TRUE, height=TRUE)
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
b1=Button(self, text="7", command=self.calculate,bg="lime")
b1.grid(column=1, row=4, columnspan=1, rowspan=1, padx=0, pady=0, ipadx=0, ipady=0, sticky='we')
''' configuring adjustability of column'''
self.columnconfigure(1, minsize=10, pad=0,weight=1)
''' configuring adjustability of rows'''
self.rowconfigure(4, minsize=10, pad=0,weight=1)
app = Application(master=root)
app.mainloop()on(master=root)
app.mainloop()
这是输出
调整大小之前[与编译后出现的相同]
调整后
如您所见,我可以调整宽度但不能调整高度。为什么会这样???
你必须用sticky='news'
让它粘在四面八方——上(north),右(east), left (west), botton (south).
几乎相同的代码,只有很少的注释
import tkinter as tk # popular method to make it shorter
#from tkinter import * # PEP8: `import *` is not preferred
# --- classes ---
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.grid(sticky="ewns")
self.feet = tk.StringVar()
self.meters = tk.StringVar()
self.create_widgets()
def calculate(self):
try:
self.meters.set(int(0.3048 * 10000.0 + 0.5)/10000.0)
except ValueError:
print("value error") # it is good to see problems
def create_widgets(self):
self.master.resizable(width=True, height=True)
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
b1 = tk.Button(self, text="7", command=self.calculate, bg="lime")
b1.grid(column=1, row=4, columnspan=1, rowspan=1, padx=0, pady=0, ipadx=0, ipady=0, sticky='news') # <-- `news`
# configuring adjustability of column
self.columnconfigure(1, minsize=10, pad=0,weight=1)
# configuring adjustability of rows
self.rowconfigure(4, minsize=10, pad=0,weight=1)
# --- main ---
root = tk.Tk()
root.title("Demo")
app = Application(root)
app.mainloop()
import tkinter
from tkinter import *
root = Tk()
root.title("Demo")
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.grid(sticky="ewns")
self.feet = StringVar()
self.meters = StringVar()
self.create_widgets()
def calculate(self):
try:
self.meters.set(int(0.3048 * 10000.0 + 0.5)/10000.0)
except ValueError:
pass
def create_widgets(self):
self.master.resizable(width=TRUE, height=TRUE)
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
b1=Button(self, text="7", command=self.calculate,bg="lime")
b1.grid(column=1, row=4, columnspan=1, rowspan=1, padx=0, pady=0, ipadx=0, ipady=0, sticky='we')
''' configuring adjustability of column'''
self.columnconfigure(1, minsize=10, pad=0,weight=1)
''' configuring adjustability of rows'''
self.rowconfigure(4, minsize=10, pad=0,weight=1)
app = Application(master=root)
app.mainloop()on(master=root)
app.mainloop()
这是输出
调整大小之前[与编译后出现的相同]
调整后
如您所见,我可以调整宽度但不能调整高度。为什么会这样???
你必须用sticky='news'
让它粘在四面八方——上(north),右(east), left (west), botton (south).
几乎相同的代码,只有很少的注释
import tkinter as tk # popular method to make it shorter
#from tkinter import * # PEP8: `import *` is not preferred
# --- classes ---
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.grid(sticky="ewns")
self.feet = tk.StringVar()
self.meters = tk.StringVar()
self.create_widgets()
def calculate(self):
try:
self.meters.set(int(0.3048 * 10000.0 + 0.5)/10000.0)
except ValueError:
print("value error") # it is good to see problems
def create_widgets(self):
self.master.resizable(width=True, height=True)
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
b1 = tk.Button(self, text="7", command=self.calculate, bg="lime")
b1.grid(column=1, row=4, columnspan=1, rowspan=1, padx=0, pady=0, ipadx=0, ipady=0, sticky='news') # <-- `news`
# configuring adjustability of column
self.columnconfigure(1, minsize=10, pad=0,weight=1)
# configuring adjustability of rows
self.rowconfigure(4, minsize=10, pad=0,weight=1)
# --- main ---
root = tk.Tk()
root.title("Demo")
app = Application(root)
app.mainloop()