Python TkInter:带包装管理器的方形按钮

Python TkInter:带包装管理器的方形按钮,python,tkinter,Python,Tkinter,如何创建方形按钮?我正在使用包管理器,但我不想更改为网格或其他 from tkinter import* import time def close(): root.destroy() root = Tk() root.configure(background="blue") root.attributes('-fullscreen', True) frame1 = Frame(root) frame1.pack(side=TOP, fill=X, anchor=N) close

如何创建方形按钮?我正在使用包管理器,但我不想更改为网格或其他

from tkinter import*
import time

def close():
    root.destroy()

root = Tk()
root.configure(background="blue")
root.attributes('-fullscreen', True)

frame1 = Frame(root)
frame1.pack(side=TOP, fill=X, anchor=N)

close_button = Button(frame1, width=5, bg='red', fg='white', font=('Helvetica', 10), text='X', command=close)
close_button.pack(side=RIGHT, anchor=E)

frame2 = Frame(root, height=100, bg='red')
frame2.pack(side=TOP, fill=X, anchor=N)

video_button = Button(frame2, width=5, fg='white', font=('Helvetica', 10), text='X', command=close)
video_button.pack(side=RIGHT, anchor=E)

music_button = Button(frame2, width=5, bg='red', fg='white', font=('Helvetica', 10), text='X', command=close)
music_button.pack(side=RIGHT, anchor=E)

root.mainloop()

提前谢谢。

您的意思是希望宽度和高度相等

按钮很有趣,因为宽度和高度是用字符设置的,所以它取决于你的字体

您可以通过添加图像来覆盖该行为,因此可以以像素为单位设置边长

import tkinter as tk

class SquareButton(tk.Button):
    def __init__(self, master=None, **kwargs):
        self.img = tk.PhotoImage()
        side = kwargs.pop('side_length', None)
        tk.Button.__init__(self, master, image=self.img, compound='center', **kwargs)
        if side:
            self.config(width=side, height=side)

def close():
    root.destroy()

root = tk.Tk()

frame1 = tk.Frame(root)
frame1.pack(side=tk.TOP, fill=tk.X, anchor=tk.N)

close_button = SquareButton(frame1, side_length=200, bg='red', fg='white', font=('Helvetica', 10), text='X', command=close)
close_button.pack(side=tk.RIGHT, anchor=tk.E)

frame2 = tk.Frame(root, height=100, bg='red')
frame2.pack(side=tk.TOP, fill=tk.X, anchor=tk.N)

video_button = SquareButton(frame2, side_length=200, fg='white', font=('Helvetica', 10), text='X', command=close)
video_button.pack(side=tk.RIGHT, anchor=tk.E)

music_button = SquareButton(frame2, side_length=200, bg='red', fg='white', font=('Helvetica', 10), text='X', command=close)
music_button.pack(side=tk.RIGHT, anchor=tk.E)

root.mainloop()

你是说你想让宽度和高度相等

按钮很有趣,因为宽度和高度是用字符设置的,所以它取决于你的字体

您可以通过添加图像来覆盖该行为,因此可以以像素为单位设置边长

import tkinter as tk

class SquareButton(tk.Button):
    def __init__(self, master=None, **kwargs):
        self.img = tk.PhotoImage()
        side = kwargs.pop('side_length', None)
        tk.Button.__init__(self, master, image=self.img, compound='center', **kwargs)
        if side:
            self.config(width=side, height=side)

def close():
    root.destroy()

root = tk.Tk()

frame1 = tk.Frame(root)
frame1.pack(side=tk.TOP, fill=tk.X, anchor=tk.N)

close_button = SquareButton(frame1, side_length=200, bg='red', fg='white', font=('Helvetica', 10), text='X', command=close)
close_button.pack(side=tk.RIGHT, anchor=tk.E)

frame2 = tk.Frame(root, height=100, bg='red')
frame2.pack(side=tk.TOP, fill=tk.X, anchor=tk.N)

video_button = SquareButton(frame2, side_length=200, fg='white', font=('Helvetica', 10), text='X', command=close)
video_button.pack(side=tk.RIGHT, anchor=tk.E)

music_button = SquareButton(frame2, side_length=200, bg='red', fg='white', font=('Helvetica', 10), text='X', command=close)
music_button.pack(side=tk.RIGHT, anchor=tk.E)

root.mainloop()

好的,现在我明白了。但是否可以在没有预设的情况下导入tkinter?(我的意思不是“将tkinter导入为”从tkinter导入*“写入”)实际上,大小设置取决于字体XD
,而从tkinter导入*
被称为“通配符导入”,这太荒谬了。它会导致难以阅读的代码和bug,因此它被认为是不好的做法,并且与PEP8背道而驰。所以我不推荐它,但是如果你想的话,你可以从所有东西中删除
tk.
前缀。实际上,高度和宽度与字体成比例是非常有用的。Tkinter旨在为您找出大部分布局。如果你以像素为单位设置大小,然后有人改变了字体,你会丢失一些按钮文本。好了,现在我知道了。但是否可以在没有预设的情况下导入tkinter?(我的意思不是“将tkinter导入为”从tkinter导入*“写入”)实际上,大小设置取决于字体XD
,而从tkinter导入*
被称为“通配符导入”,这太荒谬了。它会导致难以阅读的代码和bug,因此它被认为是不好的做法,并且与PEP8背道而驰。所以我不推荐它,但是如果你想的话,你可以从所有东西中删除
tk.
前缀。实际上,高度和宽度与字体成比例是非常有用的。Tkinter旨在为您找出大部分布局。如果以像素为单位设置大小,然后有人更改了字体,则会丢失一些按钮文本。