Python Forum
[Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window)
#3
You could create a dictionary that contains all of the colors, and use that
the following code will create a dictionary that contains: color name, Red, Green, and blue values for all tkinter colors

GetColorList.py
import requests
from bs4 import BeautifulSoup
from CreateDict import CreateDict


class GetColorList:
    def __init__(self):
        self.cd = CreateDict()
        self.color_list = {}
    
    def get_color_list(self):
        if not(len(self.color_list)):
            soup = self.get_color_page()
            if soup:
                self.parse_colors(soup)
            # self.cd.display_dict(self.color_list)
        return self.color_list

    def get_color_page(self):
        url = 'https://www.tcl.tk/man/tcl/TkCmd/colors.html'

        response = requests.get(url)
        if response.status_code == 200:
            return BeautifulSoup(response.content, 'lxml')
        else:
            print(f"Unable to find color list at {url}")
            return None

    def show_colors(self):
        for color, rgb in self.color_list.items():
            print(f"{color}: R: {rgb['red']} G: {rgb['green']} B:{rgb['blue']}")

    def parse_colors(self, soup):
        selector = "body > dl:nth-child(6) > dd:nth-child(1) > table:nth-child(1)"
        cnode = None

        table = soup.select(selector)[0]        
        trlist = table.find_all('tr')
        for n, tr in enumerate(trlist):
            if n == 0:
                continue
            tdlist = tr.find_all('td')
            for n1, td in enumerate(tdlist):
                if n1 == 0:
                    color = td.p.text.strip()
                    cnode = self.cd.add_node(self.color_list, color)
                elif n1 == 1:
                    value = int(td.p.text.strip())
                    self.cd.add_cell(cnode, 'red', value)
                elif n1 == 2:
                    value = int(td.p.text.strip())
                    self.cd.add_cell(cnode, 'green', value)
                elif n1 == 3:
                    value = int(td.p.text.strip())
                    self.cd.add_cell(cnode, 'blue', value)

def main():
    gcl = GetColorList()
    gcl.get_color_list()


if __name__ == '__main__':
    main()
it requires the following module:
CreateDict.py
import os

class CreateDict:
    """
    Generic Software tools used by Trailmapper.

    CreateDict.py - Contains methods to simplify node and cell creation within
                    a dictionary

    Usage: 
    
        The best way to learn what can be done is to examine the testit function
        included in this module.

        new_dict(dictname) - Creates a new dictionary instance with the name
            contained in dictname

        add_node(parent, nodename) - Creates a new node (nested dictionary)
            named in nodename, in parent dictionary.

        add_cell(nodename, cellname, value) - Creates a leaf node within node
            named in nodename, with a cell name of cellname, and value of value.

        display_dict(dictname) - Recursively displays a nested dictionary.

    Requirements:

        Trailmapper software:
            None

        Python standard library:
            os
    
    Author: Larz60+  -- May 2019.
    """
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))

    def new_dict(self, dictname):
        setattr(self, dictname, {})

    def add_node(self, parent, nodename):
        node = parent[nodename] = {}
        return node

    def add_cell(self, nodename, cellname, value):
        cell =  nodename[cellname] = value
        return cell

    def display_dict(self, dictname, level=0):
        indent = " " * (4 * level)
        for key, value in dictname.items():
            if isinstance(value, dict):
                print(f'\n{indent}{key}')
                level += 1
                self.display_dict(value, level)
            else:
                print(f'{indent}{key}: {value}')
            if level > 0:
                level -= 1


def testit():
    # instantiate class
    cd = CreateDict()

    # create new dictionary named CityList
    cd.new_dict('CityList')

    # add node Boston
    boston = cd.add_node(cd.CityList, 'Boston')
    # add sub node Resturants
    bos_resturants = cd.add_node(boston, 'Resturants')

    # Add subnode 'Spoke Wine Bar' to parent bos_resturants
    spoke = cd.add_node(bos_resturants, 'Spoke Wine Bar')
    cd.add_cell(spoke, 'Addr1', '89 Holland St')
    cd.add_cell(spoke, 'City', 'Sommerville')
    cd.add_cell(spoke, 'Addr1', '02144')
    cd.add_cell(spoke, 'Phone', '617-718-9463')

    # Add subnode 'Highland Kitchen' to parent bos_resturants
    highland = cd.add_node(bos_resturants, 'Highland Kitchen')
    cd.add_cell(highland, 'Addr1', '150 Highland Ave')
    cd.add_cell(highland, 'City', 'Sommerville')
    cd.add_cell(highland, 'ZipCode', '02144')
    cd.add_cell(highland, 'Phone', '617-625-1131')

    # display dictionary
    print(f'\nCityList Dictionary')
    cd.display_dict(cd.CityList)
    print(f'\nraw data: {cd.CityList}')

if __name__ == '__main__':
    testit()
Example Use:
from GetColorList import GetColorList

def show_all_colors():
    gc = GetColorList()
    color_list = gc.get_color_list()
    gc.show_colors()

show_all_colors()
partial output from example:
Output:
alice blue: R: 240 G: 248 B:255 AliceBlue: R: 240 G: 248 B:255 antique white: R: 250 G: 235 B:215 AntiqueWhite: R: 250 G: 235 B:215 AntiqueWhite1: R: 255 G: 239 B:219 AntiqueWhite2: R: 238 G: 223 B:204 AntiqueWhite3: R: 205 G: 192 B:176 AntiqueWhite4: R: 139 G: 131 B:120 agua: R: 0 G: 255 B:255 aquamarine: R: 127 G: 255 B:212 aquamarine1: R: 127 G: 255 B:212 aquamarine2: R: 118 G: 238 B:198 aquamarine3: R: 102 G: 205 B:170 aquamarine4: R: 69 G: 139 B:116 azure: R: 240 G: 255 B:255 azure1: R: 240 G: 255 B:255 azure2: R: 224 G: 238 B:238 azure3: R: 193 G: 205 B:205 azure4: R: 131 G: 139 B:139 beige: R: 245 G: 245 B:220 bisque: R: 255 G: 228 B:196 bisque1: R: 255 G: 228 B:196 bisque2: R: 238 G: 213 B:183 bisque3: R: 205 G: 183 B:158 bisque4: R: 139 G: 125 B:107 black: R: 0 G: 0 B:0 blanched almond: R: 255 G: 235 B:205 BlanchedAlmond: R: 255 G: 235 B:205 blue: R: 0 G: 0 B:255 blue violet: R: 138 G: 43 B:226
Reply


Messages In This Thread
RE: Open tkinter colorchooser at toplevel (so I can select/focus on either window) - by Larz60+ - Jul-05-2022, 09:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter multiple windows in the same window hosierycouch 1 1,231 May-30-2024, 04:28 AM
Last Post: deanhystad
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 2,534 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 2,163 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  pass a variable between tkinter and toplevel windows janeik 9 6,166 Oct-05-2023, 04:22 AM
Last Post: janeik
  Centering and adding a push button to a grid window, TKinter Edward_ 15 14,289 May-25-2023, 07:37 PM
Last Post: deanhystad
  Now if window is open or not delcencen 5 4,124 Mar-25-2023, 07:26 PM
Last Post: deanhystad
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 2,934 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  RGB() <> colorchooser.askcolor KennethHobson 9 4,059 Aug-24-2022, 06:43 PM
Last Post: woooee
  [Tkinter] Toplevel window menator01 5 5,588 Apr-18-2022, 06:01 PM
Last Post: menator01
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 4,969 Apr-16-2022, 04:04 PM
Last Post: DBox

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020