Python Forum
Problems getting tk Combobox contents to populate properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems getting tk Combobox contents to populate properly
#1
I could use some help with tkinter comboboxes, and populating the combobox from a database. First question is, will the combobox accept a dictionary like key/value structure? Like (1,"Task Example 1"). I want to select based on a shown phrase, but return and do further processing with an associated index/key value. If the tkinter combobox can't do that, I have to try some other GUI framework.

Second question is, what is the proper phrasing to populate a combobox when taking data from a dataframe? I've tried everything, including converting a dataframe to a dictionary, and can't get it to work properly. Usually I get one column, with a separate line item for each word of the phrase, rather than the whole phrase on a line. I'm tried what seems like scores of different addressing approaches to the dataframe, with no luck, so I just left the addressing/slice info off below.

I've tried tracking down answers on several websites, including this one, but the information is always specific to that poster's code, and never seems transferable to mine.

import sqlite3
import pandas as pd
import tkinter as tk
from tkinter import ttk

cnxn= sqlite3.connect('E:/taskmanager/taskmanager.db')

cbodf=pd.read_sql_query('Select TaskID, Headline from MainTasks', cnxn)
root=tk.Tk()
root.title("Task Manager")
root.geometry("500x300+50+50")
current_var = tk.StringVar()
cboTask = ttk.Combobox(root, textvariable=current_var)

cboTask['values']=cbodf
cboTask.pack(fill=tk.X, padx=5, pady=15)

root.mainloop()
Here is some sample data, in case someone needed it:

"TaskID","Requester","Headline"
6,"David","Set up SQL Server to use Python"
8,"David","Make Python talk to sql server"
Reply
#2
I made some progress in getting things to populate by using fetchall() instead of a dataframe, but I'm still getting my key value concatenated to the text, whereas I'd like that hidden, but accessible through code. Here's my new code:

import sqlite3
import pandas as pd
import tkinter as tk
from tkinter import ttk

cnxn= sqlite3.connect('E:/taskmanager/taskmanager.db')
c=cnxn.cursor()
c.execute('Select TaskID, Headline from MainTasks')

root=tk.Tk()
root.title("Task Manager")
root.geometry("500x300+50+50")
current_var = tk.StringVar()
cboTask = ttk.Combobox(root, textvariable=current_var)
cboTask['values']=c.fetchall()

cboTask.pack(fill=tk.X, padx=5, pady=15)

root.mainloop()
Reply
#3
Might could try something like this

#! /usr/bin/env python3

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root['padx'] = 5
root['pady'] = 4

def update(*args):
    label['text'] = f'Key -> {key.get()} | Value -> {mydict[key.get()]}'

mydict = {'A':5, 'B': 2, 'C': 1, 'D': 3, 'E': 4, 'F': 0}
data = list(mydict.keys())

key = tk.StringVar()
combobox = ttk.Combobox(root, values=data, textvariable=key)
combobox.pack(side='top')

key.trace('w', update)

label = tk.Label(root)
label.pack(side='left')
root.mainloop()
BashBedlam likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
Thank you menator01. Will try to adapt it to what I have.
Reply
#5
Ok, was able to meld this with my database code. Keys and values seem to be swapped, but I compensated and it is working visually. I get an error if I put them the way I thought they should go.
Thanks again.

# -*- coding: utf-8 -*-
"""
Created on Fri Jan  7 17:23:52 2022

@author: DSNoS
"""

import sqlite3
import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.title("Task Manager")
root.geometry("500x300+50+50")

def update(*args):
    label['text'] = f'Key -> {key.get()} | Value -> {mydict[key.get()]}'
    

cnxn= sqlite3.connect('E:/taskmanager/taskmanager.db')
c=cnxn.cursor()
c.execute('Select Headline, TaskID from MainTasks')

mydict=dict(c.fetchall())

data = list(mydict.keys())

key = tk.StringVar()

cboTask = ttk.Combobox(root, textvariable=key , values=data)

cboTask.pack(fill=tk.X, padx=5, pady=15)

key.trace('w', update)
label = tk.Label(root)
label.pack(side='left')
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] populate a QListWidget devilonline 1 1,584 Apr-10-2023, 02:52 AM
Last Post: deanhystad
  [PyQt] [Solved]Populate ComboBox with for loop? Extra 3 2,143 Jul-31-2022, 09:01 PM
Last Post: Extra
  Problems with ValueMember vs DisplayMember with combobox dford 7 2,362 Feb-23-2022, 11:05 PM
Last Post: deanhystad
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,395 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt
  Auto populate dictionary with names/values of QT widgets cjh 1 2,944 Mar-23-2021, 02:56 PM
Last Post: deanhystad
  [PyQt] How to populate a treeview on a GUI with a dictionary mart79 1 8,254 Aug-05-2019, 01:30 PM
Last Post: Denni
  populate list with images and be able to select them ricardons 0 2,131 Jan-11-2019, 03:45 PM
Last Post: ricardons
  Hardest time getting DataFrame to populate Tree Widget WuchaDoin 4 7,371 Oct-15-2018, 08:29 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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