【问题标题】:How do I place a text entry in a colored frame in python using tkinter?如何使用 tkinter 在 python 中的彩色框架中放置文本条目?
【发布时间】:2019-08-30 05:09:12
【问题描述】:

我正在使用 Tkinter 在 python 中为 MacOS 制作一个线框工具桌面应用程序,但我不知道如何有一个文本输入栏,我可以将它放入背景颜色为黑色的框架中。

我查找了如何尝试执行此任务,但没有运气。我也试过问我的编码班老师是否可以帮我解决这个问题,但他也想不通。有人可以帮我解决这个问题吗?

这是我目前所拥有的:

from tkinter import *

root = Tk()
root.geometry("1430x840")


def clear_entry(entry):
   entry.delete(0, END)

// here is the text entry
entry = Entry(root)
placeholder_text = 'Title'
entry.insert(0, placeholder_text)
entry.bind("<Button-1>", lambda event: clear_entry(entry))


// here is the frame which I want to put it in
frame2 = Frame(root, width=1430, height=56, bg="#292E30")
frame2.pack()
entry.pack(side=TOP, anchor=N)


root.mainloop()

我想要的最终结果,这是带有预览功能的编辑图像,因此我可以向您展示我希望它最终的样子。

【问题讨论】:

    标签: python-3.x macos python-2.7 tkinter


    【解决方案1】:

    只需将您的frame2 向上移动并将入口主设置为frame2。要获得完全拉伸的黑框,请向您的 pack 方法传递更多参数:

    from tkinter import *
    
    root = Tk()
    root.geometry("1430x840")
    
    frame2 = Frame(root, bg="#292E30")
    frame2.pack(fill=X,ipady=15)
    
    def clear_entry(entry):
        entry.delete(0, END)
    
    entry = Entry(frame2)
    placeholder_text = 'Title'
    entry.insert(0, placeholder_text)
    entry.bind("<Button-1>", lambda event: clear_entry(entry))
    
    entry.pack(side=LEFT, anchor=N)
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 2016-08-04
      • 2011-12-24
      • 2021-12-01
      相关资源
      最近更新 更多