Python tkinter 用一个按钮改变背景颜色
Python tkinter change background color with a button
这是我的代码的一部分,我试图在其中创建一个用户输入字段,用户可以在其中输入他们想要的背景颜色类型,然后单击它下面的按钮并实现它。
我使用了完全相同的代码来改变画笔的颜色"
create_oval(color, outline)
还有效,它似乎不影响 bg 颜色,有什么建议吗?
import tkinter
background = "white"
okno = tkinter.Tk()
okno.title("Project")
platno = tkinter.Canvas(okno, height = 300, width = 300, bg = background)
platno.pack()
def background_color():
background = vstup2.get()
vstup2.set(background)
tkinter.Label(okno,text = "Background color :", bg = "white", width = 30).pack()
vstup2 = tkinter.StringVar()
tkinter.Entry(okno,textvariable = vstup2, ).pack()
tkinter.Button(okno,width=30, text="Set the color of a background", command=background_color).pack()
我使用 .config()
函数修复了您的代码。在背景更改功能中,您不要尝试更改背景。您只更改 StringVar()
,无论如何都不会更改背景。
我还让你的 gui 看起来更好,像这样:
import tkinter
background = "white"
okno = tkinter.Tk()
okno.title("Project")
okno.config(bg = "white")
platno = tkinter.Canvas(okno, height = 300, width = 300, bg = background, highlightthickness = 0)
platno.pack()
def background_color():
background = vstup2.get()
try:
platno.config(bg = background)
except:
pass
tkinter.Label(okno,text = "Background color :", bg = "white", width = 30).pack()
vstup2 = tkinter.StringVar()
tkinter.Entry(okno,textvariable = vstup2, bg = "white").pack()
tkinter.Button(okno,width=30, text="Set the color of a background", command=background_color, relief = "flat", activebackground = "white", bd = 0, bg = "white").pack()
okno.mainloop()
输出:
您还必须在末尾添加一个 .mainloop()
。在某些文本编辑器中,如果您不添加它,程序将无法正常 运行。
希望对您有所帮助!
这是我的代码的一部分,我试图在其中创建一个用户输入字段,用户可以在其中输入他们想要的背景颜色类型,然后单击它下面的按钮并实现它。
我使用了完全相同的代码来改变画笔的颜色"
create_oval(color, outline)
还有效,它似乎不影响 bg 颜色,有什么建议吗?
import tkinter
background = "white"
okno = tkinter.Tk()
okno.title("Project")
platno = tkinter.Canvas(okno, height = 300, width = 300, bg = background)
platno.pack()
def background_color():
background = vstup2.get()
vstup2.set(background)
tkinter.Label(okno,text = "Background color :", bg = "white", width = 30).pack()
vstup2 = tkinter.StringVar()
tkinter.Entry(okno,textvariable = vstup2, ).pack()
tkinter.Button(okno,width=30, text="Set the color of a background", command=background_color).pack()
我使用 .config()
函数修复了您的代码。在背景更改功能中,您不要尝试更改背景。您只更改 StringVar()
,无论如何都不会更改背景。
我还让你的 gui 看起来更好,像这样:
import tkinter
background = "white"
okno = tkinter.Tk()
okno.title("Project")
okno.config(bg = "white")
platno = tkinter.Canvas(okno, height = 300, width = 300, bg = background, highlightthickness = 0)
platno.pack()
def background_color():
background = vstup2.get()
try:
platno.config(bg = background)
except:
pass
tkinter.Label(okno,text = "Background color :", bg = "white", width = 30).pack()
vstup2 = tkinter.StringVar()
tkinter.Entry(okno,textvariable = vstup2, bg = "white").pack()
tkinter.Button(okno,width=30, text="Set the color of a background", command=background_color, relief = "flat", activebackground = "white", bd = 0, bg = "white").pack()
okno.mainloop()
输出:
您还必须在末尾添加一个 .mainloop()
。在某些文本编辑器中,如果您不添加它,程序将无法正常 运行。
希望对您有所帮助!