如何使用左对齐方式控制tkinter按钮的宽度?

huangapple go评论93阅读模式
英文:

How to control tkinter Button width with left alignment?

问题

考虑以下:

from tkinter import mainloop, Button
import tkinter as tk

def cmd():
    print('Hello World')

class my_class(tk.Tk):
    def __init__(self):
        super().__init__()
        Button(self, text="Button A", command=cmd, anchor="w", justify="left").place(x=10, y=10)
        Button(self, text="Button B has a long name", command=cmd, anchor="w", justify="left").place(x=10, y=60)
        Button(self, text="Button C has a long name", command=cmd, width=25, anchor="w", justify="left").place(x=10, y=110)
        Button(self, text="Button D", command=cmd, width=25, anchor="w", justify="left").place(x=10, y=160)

if __name__ == '__main__':
    app = my_class()
    app.geometry('400x300')
    app.mainloop()
英文:

Consider this:

from tkinter import mainloop, Button
import tkinter as tk

def cmd():
    print('Hello World')

class my_class(tk.Tk):
    def __init__(self):
        super().__init__()
        Button(self, text="Button A", command = cmd ).place(x=10, y=10)
        Button(self, text="Button B has a long name", command = cmd).place(x=10, y=60)
        Button(self, text="Button C has a long name", command = cmd, width = 25 ).place(x=10, y=110)
        Button(self, text="Button D", command = cmd, width = 25 ).place(x=10, y=160) 
           
    
if __name__ == '__main__':
    app = my_class()
    app.geometry = ('400x300')
    app.mainloop()

That give the following.

如何使用左对齐方式控制tkinter按钮的宽度?

Text in the two upper buttons is left aligned, but the buttons have different width.
Text in the two lower buttons is centered in the buttons, but the buttons have the same width.
How do I make text in all the buttons left aligned AND make all the button the same width?

答案1

得分: 1

Button(self, text="Button A", command=cmd, width=25, anchor='w').place(x=10, y=10)
Button(self, text="Button B has a long name", command=cmd, width=25, anchor='w').place(x=10, y=60)
Button(self, text="Button C has a long name", command=cmd, width=25, anchor='w').place(x=10, y=110)
Button(self, text="Button D", command=cmd, width=25, anchor='w').place(x=10, y=160)
英文:

"How do I make text in all the buttons left aligned AND make all the button the same width?"

Set all buttons to the same width and anchor them to the "w"est

from tkinter import mainloop, Button
import tkinter as tk

def cmd():
    print('Hello World')

class my_class(tk.Tk):
    def __init__(self):
        super().__init__()
        Button(self, text="Button A", command = cmd, width=25, anchor = 'w').place(x=10, y=10)
        Button(self, text="Button B has a long name", command = cmd, width = 25, anchor = 'w').place(x=10, y=60)
        Button(self, text="Button C has a long name", command = cmd, width = 25, anchor = 'w' ).place(x=10, y=110)
        Button(self, text="Button D", command = cmd, width = 25, anchor = 'w' ).place(x=10, y=160)


if __name__ == '__main__':
    app = my_class()
    app.geometry = ('400x300')
    app.mainloop()

如何使用左对齐方式控制tkinter按钮的宽度?

huangapple
  • 本文由 发表于 2023年4月17日 22:18:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定