英文:
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.
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论