【问题标题】:How can I set a text left in a button with an image如何在带有图像的按钮中设置文本
【发布时间】:2019-06-13 17:37:01
【问题描述】:

我的问题是将文本对齐设置为按钮的左侧。 按钮是一个图像。使用我的代码,文本设置在中心。

root = tk.Tk()                                               
root.geometry('740x740+200+200') 

img = PhotoImage(file="/Users/my_name/Documents/Wing101 Beispiele/button-2.png") 

testBtn1=Button(root,  fg="white", image=img, text="TEST1",  compound=tk.CENTER, command=lambda: TestLogic(0)).place(x=400, y=200) 

root.mainloop()

【问题讨论】:

    标签: button text tkinter alignment


    【解决方案1】:

    查看此修改后的代码。

    您需要在文本右侧添加空格。要添加的空格量取决于tk.Button 的宽度

    我确实尝试为tk.Button 添加justify="left" 选项,但没有发现它有帮助。不知道为什么。

    import tkinter as tk
    
    root = tk.Tk()                                               
    root.geometry('740x740+200+200') 
    
    img = tk.PhotoImage(file="/Users/my_name/Documents/Wing101 Beispiele/button-2.png") 
    
    text="TEST1                                                                                         "
    testBtn1=tk.Button(root,fg="blue",image=img,text=text,compound=tk.CENTER,
                       command=lambda: TestLogic(0)).place(x=400, y=200) 
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多