[英]Changing the colour of tkinter rectangles using Entry and button
Super new to coding, I have a tkinter GUI with 5 rectangles, an entry box, and a button labelled go. 对于编码来说是超级新手,我有一个带有5个矩形的tkinter GUI,一个输入框和一个标记为go的按钮。 I want to be able to change the color of the rectangles based on the number I enter into the entry box after I hit go.
我希望能够根据我点击go后在输入框中输入的数字来更改矩形的颜色。 I don't think i'm close?
我不觉得我很近吗? but i have no idea how to proceed.
但我不知道如何进行。 Help would be appreciated.
帮助将不胜感激。 Thanks
谢谢
from tkinter import *
the_window = Tk()
the_window.title('Show Text Count')
def changelbl():
number=(numberx.get())
if (numberx == 1):
label1.config(fill='green')
numberx=StringVar()
canvas = Canvas(the_window, width=270, height=20)
canvas.pack()
label1 = canvas.create_rectangle(0, 0, 50, 20, fill='grey')
label2 = canvas.create_rectangle(55, 0, 105, 20, fill='grey')
label3 = canvas.create_rectangle(110, 0, 160, 20, fill='grey')
label4 = canvas.create_rectangle(165, 0, 215, 20, fill='grey')
label5 = canvas.create_rectangle(220, 0, 270, 20, fill='grey')
Entrybox = Entry(the_window, bg='grey', width=15, textvariable=numberx)
Entrybox.pack(padx=(60,0), side=LEFT)
Gobutton = Button(the_window, text='Go',command=changelbl)
Gobutton.config(height=1, width=5)
Gobutton.pack(padx=(15,0), side=LEFT)
Try using canvas.itemconfig(label, fill = 'whatever')
. 尝试使用
canvas.itemconfig(label, fill = 'whatever')
。
Also, you needed to fix the indentation after defining a function. 另外,您需要在定义函数后修复缩进。
change if number == "1"
because the value of stringVar()
will be a string. if number == "1"
更改if number == "1"
因为stringVar()
的值将是一个字符串。
def changelbl():
number = (numberx.get())
if number == '1':
canvas.itemconfig(label1, fill='green')
Here is a corrected version that changes the rectangle colors upon entry of a number in the entrybox
, and pressing the Go button
: 下面是改变后的号码条目的矩形颜色校正版本
entrybox
,并按下Go button
:
from tkinter import *
the_window = Tk()
the_window.title('Show Text Count')
def on_Go_button():
try:
num = int(entrybox.get())
except:
num = 3
num = num % 5
canvas.itemconfig(num, fill='green')
numberx=StringVar()
canvas = Canvas(the_window, width=270, height=20)
canvas.pack()
pos = [(0, 0, 50, 20), (55, 0, 105, 20), (110, 0, 160, 20), (165, 0, 215, 20), (220, 0, 270, 20)]
rectangles = [None for _ in range(5)]
for idx in range(5):
rectangles[idx] = canvas.create_rectangle(*pos[idx], fill='grey')
entrybox = Entry(the_window, bg='grey', width=15, textvariable=numberx)
entrybox.pack(padx=(60,0), side=LEFT)
gobutton = Button(the_window, text='Go',command=on_Go_button)
gobutton.config(height=1, width=5)
gobutton.pack(padx=(15,0), side=LEFT)
the_window.mainloop()
This may not be as simple of an answer as you were looking for, but hopefully it will be of some help. 这可能不是您想要的答案那么简单,但是希望它将对您有所帮助。
I thought that creating a class would be a better approach for getting your desired result. 我认为创建一个类将是获得所需结果的更好方法。 I also changed
changelbl
to allow for multiple color options to be retrieved from a dictionary. 我还更改了
changelbl
以允许从字典中检索多种颜色选项。
In your code, you were missing the line, the_window.mainloop()
which will start start running your tkinter window. 在您的代码中,您缺少
the_window.mainloop()
这一行,它将开始开始运行您的the_window.mainloop()
窗口。
from tkinter import *
class ColorBoxes:
def __init__(self, the_window):
self.root = the_window
self.root.title('Show Text Count')
self.canvas = Canvas(self.root, width=270, height=20)
self.canvas.pack()
self.label1 = self.canvas.create_rectangle(0, 0, 50, 20, fill='grey')
self.label2 = self.canvas.create_rectangle(55, 0, 105, 20, fill='grey')
self.label3 = self.canvas.create_rectangle(110, 0, 160, 20, fill='grey')
self.label4 = self.canvas.create_rectangle(165, 0, 215, 20, fill='grey')
self.label5 = self.canvas.create_rectangle(220, 0, 270, 20, fill='grey')
self.entry_box = Entry(self.root, bg='grey', width=15,)
self.entry_box.pack(padx=60, side=LEFT)
self.go_button = Button(self.root, text='Go', command=self.changelbl)
self.go_button.config(height=1, width=5)
self.go_button.pack(padx=15, side=LEFT)
def changelbl(self):
number = self.entry_box.get()
fill_colors = {
'1': 'green',
'2': 'yellow',
'3': 'red'
# can add more colors to this dictionary if needed
}
self.canvas.itemconfig(self.label1, fill=fill_colors.get(number))
self.canvas.itemconfig(self.label2, fill=fill_colors.get(number))
self.canvas.itemconfig(self.label3, fill=fill_colors.get(number))
self.canvas.itemconfig(self.label4, fill=fill_colors.get(number))
self.canvas.itemconfig(self.label5, fill=fill_colors.get(number))
the_window = Tk()
ColorBoxes(the_window)
the_window.mainloop()
Again, this may be more than you were looking for, but hopefully you can pick up on what is going on and learn a bit! 同样,这可能比您想要的要多,但希望您能了解正在发生的事情并学到一些知识! Good luck!
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.