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

时间:2017-04-21 23:32:38

标签: python tkinter

如何创建方形按钮?我正在使用包经理,但我不想改为网格等。

 $connection = mysqli_connect($servername,$username,$password,$dbname);
if(!$connection)
    die("Database connection failed: " . mysqli_connect_error());

$query = "SELECT ID FROM channel "; 

if($result = mysqli_query($connection,$query))
{
    $count = mysqli_num_rows($result);// it will start from the first row and continue with others...
    //fetch one and one row
    while($row=mysqli_fetch_row($result))
    {
        for($i=0; $i<$count; $i++)
        echo "<a>$row[$i]</a>";
    }
    //free result set 
    mysqli_free_result($result);

}
mysqli_close($connection);

提前谢谢。

1 个答案:

答案 0 :(得分:0)

你的意思是你想要宽度和高度相等?

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

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

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