0

a. Is it possible to highlight the combobox when we hover the mouse over the button ? b. Because if its menu button we have the option like activebackground which helps to highlight the menu button when the mouse is hovered over it.

I tried someoptions for the combobox but its just changing the color of the selection or listbox etc but unable to highlight the combobox when mouse moves over it.

Can anyone provide some suggestion or comments for the same ?

#!/usr/intel/bin/python2.7

import Tkinter
from Tkinter import *
from Tkinter import Tk, StringVar
import ttk
import tkFont

try:
    import Tkinter as tk
    import Tkinter.ttk as ttk
except ImportError:
    import Tkinter as tk
    import ttk


class Application:

def __init__(self, parent):
    self.parent = parent
    self.combo()

def combo(self):

    MyFontBtn = tkFont.Font(family='courier', size=20, weight=tkFont.BOLD)

    self.box_value = StringVar()
    self.box = ttk.Combobox(self.parent, textvariable=self.box_value, state='readonly', width=39, font=MyFontBtn)
    self.box['values'] = ('Default', 'User Defined', 'Load From SS')
    self.box.set("Hello Click Drop Down")
    self.box['state'] = 'readonly'
    self.box.bind("<<ComboboxSelected>>", self.print_selected_value)
    self.box.option_add('*TCombobox*Listbox.selectBackground', 'gray50')
    self.box.option_add('*TCombobox*Listbox.font', MyFontBtn)

    #self.box.option_add('TCombobox.background', 'gray50')


    style1 = ttk.Style()
    style1.map('TCombobox', selectbackground=[('readonly', 'red')])
    #style1.map('TCombobox', selectforeground=[('readonly', 'blue')])

    style1.map("self.box",
                foreground=[('pressed', 'red'), ('active', 'blue')]
                #background=[('pressed', '!disabled', 'black'), ('active', 'white')]
              )
    self.box.grid(column=0, row=0)


def print_selected_value(self, *args):
    print "Vaue selected is:", self.box.get()
Vimo
  • 1,061
  • 3
  • 12
  • 22
  • `style1.map("self.box",...)` has no effect because you never assign some control the style `self.box` and you don't `style1.configure` have a definition or layout and it is not a sub-style of an existing control like `Redbutton.TButton`. – rioV8 Jul 23 '18 at 02:26

1 Answers1

0

I managed to change the foreground color of (non)selected option. Only the selectbackground changes. The Combobox sets the hover state on mouse over.

  style1.map('TCombobox', selectforeground=[('hover', 'red')], selectbackground=[('hover', 'green')])
  style1.map('TCombobox', foreground=[('hover', 'red')], background=[('hover', 'green')])

Edit:

Below the modification to determine the state flags of a control during mouse interaction.

class Application:

  def __init__(self, parent):
      self.parent = parent
      self.combo()
      self.printState()

  def printState(self):
    print ("State Combo:", self.box.state())
    self.parent.after(1000, self.printState)
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • WHen i used the above code it says, _tkinter.TclError: Invalid state name hover – Vimo Jul 23 '18 at 01:20
  • I debugged the state of the control in case the mouse is over or it has focus. On my Windows Python 3.6 it gives me the `hover` state for the combobox. I also found out that `w['state']` gives a different output than `w.state()`. I have edited the answer with the debug code so you can try to see what the state is for your platform – rioV8 Jul 23 '18 at 02:18