473,320 Members | 1,766 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,320 software developers and data experts.

Change button color on if clause - Tkinter

2
Hi guys,
I'm new to Python, and now I'm trying to make a simple GUI with Tkinter.

So.. I want to change a button's color based on if file is selected clause:

Expand|Select|Wrap|Line Numbers
  1. import Tkinter, tkFileDialog, Tkconstants 
  2. from Tkinter import * 
  3.  
  4. version = '1.0'
  5. filetext= 'Select Landsat 8 OLI'
  6. def openFile():
  7.     filename = tkFileDialog.askopenfilename(parent=root,initialdir='/home/',title=filetext ,filetypes=[('ERDAS IMAGINE', '.img')]) ## filename not filehandle
  8.     filebut["text"]= str(filename) if filename else filetext
  9.  
  10. root = Tk() 
  11. root.title('NDVI GUI ' + version)
  12. root.geometry('300x100')
  13. #Options for buttons
  14. button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
  15.  
  16. #Define asking watermark file button
  17. filebut = Button(root, text = filetext, fg = 'black', command= openFile)
  18. filebut
  19. filebut.pack(**button_opt)
  20. root.mainloop()
  21.  
I was wondering if I could change bg to green of the
Expand|Select|Wrap|Line Numbers
  1. filebut = Button
based on the function
Expand|Select|Wrap|Line Numbers
  1. openFile
clause
Expand|Select|Wrap|Line Numbers
  1. if filename else filetext
Something like if filename == None:
bg = "green"
else pass

I know that I probably this is very bad written but just wanted to see if could be done. Thank you!
Jul 8 '16 #1
2 6185
dwblas
626 Expert 512MB
To change any widget's color, including a button, you use the config function with a "background" or foreground" parameter, some_button.config(background="green"), or access the dictionary directly, some_button["background"]="green" http://effbot.org/tkinterbook/button.htm
Jul 8 '16 #2
indeed
2
Thank you, I found that link myself. But I can't make it work..
What I want to do is to change button bg color when a condition is met. If file is selected change to green, if not don't (pass).
Jul 9 '16 #3

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

Similar topics

6
by: liglin | last post by:
The following script changes the color of text with the onmousover event. How can it be modified so it changes the text when the button is clicked? I'd want to avoid layers or CSS. Thanks,...
2
by: Grey | last post by:
how to change the button backcolor with C# in coding. I want to change back color of the button when I click it. Million Thanks..
1
by: tshad | last post by:
I want to be able to change the color of the text of a button after it is pressed. The first part of the event is: sub submitQuestion_click(Sender as Object, e as EventArgs)...
1
by: AA Arens | last post by:
How to change the color of the buttons or the button from the switchboard? I use Access2003 but the database is from <2003, so the buttons are gray and squaired. Bart
3
by: ramkishor | last post by:
How can I change the color of button on mouse move over it. help .
1
by: Garg | last post by:
Hi All, In Window XP, when the radio button is disabled it become almost invisible. How can I change the color of this radio button so that it is visible in disabled mode also. Thanks in...
0
by: slinky | last post by:
Does anyone know how I can get the following code finished. I'm trying to dreate a onmouseover event to change the color of a button. Here's my .vb: ( I included other code for the button...
0
by: id10t error | last post by:
Hello, I am making an ASP.NET page to run on a handheld scanner. The screen is pretty small and when you use the tab button to move from button it is hard to tell what button the tab index is...
2
by: dangerouskicker | last post by:
dear friends any one can guide me or help, How i can change checkbox and radio button color change with java script regards devang
1
by: snehasismishra1 | last post by:
Hi, I have two forms. Let say, form1 and form2. in form1 class, i have method ChangeColor() and i am calling this method in button click event of form2. But it is not changing the color of form2....
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.