473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to change value of label in Tkinter

I want to change the text in a label everytime the button is clicked and the command is called. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from random import *
  3.  
  4. def background():
  5.     x = randrange(255)
  6.     y = randrange(255)
  7.     z = randrange(255)
  8.     rgb_color = [x,y,z]
  9.     mycolor = '#%02x%02x%02x' % (x, y, z)
  10.     app.configure(bg=mycolor)
  11.     label1 = Label(app, text=rgb_color)
  12.     label1.pack()
  13.  
  14. app = Tk()
  15. app.geometry("500x400+5+5")
  16. app.resizable(0,0)
  17. app.title("Color Code")
  18. button1 = Button(app, text="Change", command=background)
  19. button1.pack()
  20. app.mainloop()
Every time the button is clicked, a new label is created under it. How can I make it change the current label based on the rgb_color? Thanks.
Sep 23 '14 #1
1 25022
bvdet
2,851 Expert Mod 2GB
Initialize the label outside of the callback function. Configure the label widget in the callback.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. from random import *
  3.  
  4. def background():
  5.     x = randrange(255)
  6.     y = randrange(255)
  7.     z = randrange(255)
  8.     rgb_color = [x,y,z]
  9.     mycolor = '#%02x%02x%02x' % (x, y, z)
  10.     app.configure(bg=mycolor)
  11.     label1.configure(text=rgb_color)
  12.  
  13. app = Tk()
  14. app.geometry("500x400+5+5")
  15. app.resizable(0,0)
  16. app.title("Color Code")
  17. button1 = Button(app, text="Change", command=background)
  18. button1.pack()
  19. label1 = Label(app, text="")
  20. label1.pack()
  21. app.mainloop()
Sep 23 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Matt | last post by:
I want to change the label on the fly (when the user click the checkbox), but my attempt InputForm.lataLbl.value = "LATA * "; didn't work. Here's my code, any ideas? thanks !!! <html>...
1
by: Tim Begin | last post by:
I am attempting to use the ThreadPool.SetMinThreads method as shown in the MSDN example code: int minWorker, minIOPort; int newWorker,newIOPort; ThreadPool.GetAvailableThreads(out minWorker, out...
3
by: OutdoorGuy | last post by:
Greetings, I have a very "novice" question to ask, so hopefully it will be easy. At any rate, I have a label on a Windows form called "label1". I simply want to change the "backcolor" property...
1
by: Shapper | last post by:
Hello, I have a label tag and a input tag in my page: <label for="subject">Nome:<input type="text" id="sbj"></input></label> How can I change the label value "Nome:" to "Name:" in Page_Load?...
2
by: Simon | last post by:
Dear reader, Is it possible to change the text of a label in a form on the event "By open". In the event some VBA code will change the label text.
3
by: John Smith | last post by:
I'm looking into this peace of code: protected void DropDown_SelectedIndexChanged(object sender, EventArgs e) { DropDownList list = (DropDownList)sender; TableCell cell = list.Parent as...
2
by: John Smith | last post by:
Will this line of the code: item.Cells.Text = "Some text..."; change only DataGrid visual value or it will also change value in the DataSource? How can I change value in DataSource? ...
4
by: darnnews | last post by:
When a user selects a "Medium" in a form I have made, I would like to change a Label on the form. Can I change a label based on an update of a listbox? This code does not work: Case "Print"...
0
by: marknoten | last post by:
Hi, we are working with XML files that can be as big as 6Mb. Some XML documents that obey to a certain condition must be duplicated where only 2 elements need to change value. I thought using...
1
by: PawelR | last post by:
Hi Group, In my application I have DataTable which is displayed in DataGridView via DataView: DataView myView = new DataView(myTable); myDataGridView.DataSource = myView; One column im...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.