Python Forum
textfile to customtkinter combobox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
textfile to customtkinter combobox
#3
Do not use "list" as a variable name. It is a built-in python function and the name of a python datatype. This is a bad practice but it is not the source of your problem.

Another bad practice is putting your program files in the Python directory tree. You should not be putting ipland.txt or any file you write in d:\python311\Scripts. Create a folder that is not in the Python source directory tree and write your programs there. Adding files to the Python directory tree often leads to odd behaviors that are difficult to debug, but this is not the source of your problem.

A few more bad practices that are not the source of your problem:
1. Avoid using styling in your code. Your application should be in harmony with other applications, not in blaring contrast. Customtkinter has good theme support. Use that.
2. Windows should be resizeable unless there is a good reason for them to not be.
3. Do not use geometry to set the window size. Let the window size itself to fit the contents. If the user has set a large system font, your application should automatically adjust.
4. Do not use width and height to set the widget size. Let the widget set it's own size. If multiple widgets should be the same size, use grid(sticky) or pack(expand, fill) to tell the window geometry manager to resize the widgets.
5. Do not use place() to position widgets in the window. Use pack() or grid() to position widgets in the window.
6. Use defaults. For example, window's default state is normal. You don't have to set it to normal.

The source of the problem is that you are passing a string to Combobox values. The documentation says Combobox expects a list of strings, but it appears any iterable will work. Instead of passing a list of strings, you made one long string that kind of looks like a list of strings. When you iterate a string, it returns the characters one at a time. The solution, as menator01 shows, is putting the country names in a list, not a string named "list" that is formatted to look like a list when it is printed out.

menator also shows that it is silly to write a function that is only used once. I will show how you can take your idea and use it to make a useful function. The function combo_from_file() creates a Combobox object, loading the values from a file. This function could be used to make multiple combo boxes, specifying a different values file for each one.
def combo_from_file(*args, values=None, **kwargs):
    """Create combobox using values from a file."""
    with open(values, "r") as f:   # Use context manager to automatically close file when done.
        values = [value.strip() for value in f]
    return customtkinter.CTkComboBox(*args, values=values, **kwargs)

# Use forward slashes in filenames.  Prevents "\" from accidentally being part of an ascii escape sequence.
land = combo_from_file(window, value="d:/my_folder/inpland.txt", width=200)
land.pack(padx=50, pady=50)
But I would probably make a class instead of writing a function. If I needed to make a bunch of comboboxes, and the values might not be a list, might even be read from a file, I might write a class like this:
import tkinter as tk
from tkinter import ttk


class EnhancedCombo(ttk.Combobox):
    """A combobox where values don't have to be lists."""

    def __init__(self, *args, values=[], **kwargs):
        if type(values) is str:
            # Assume strings are filenames
            with open(values, "r") as f:
                values = [value.strip() for value in f]
        elif not isinstance(values, list):
            values = list(values)
        return super().__init__(*args, values=values, **kwargs)


numbers = {"one": 1, "two": 2, "three": 3}
window = tk.Tk()
EnhancedCombo(window, values=numbers, width=60).pack(padx=10, pady=10)
EnhancedCombo(window, values=numbers.values(), width=60).pack(padx=10, pady=10)
EnhancedCombo(window, values=numbers.keys(), width=60).pack(padx=10, pady=10)
EnhancedCombo(window, values=__file__, width=60).pack(padx=10, pady=10)
window.mainloop()
Reply


Messages In This Thread
textfile to customtkinter combobox - by janeik - Aug-30-2023, 01:49 PM
RE: textfile to customtkinter combobox - by janeik - Aug-30-2023, 09:07 PM
RE: textfile to customtkinter combobox - by janeik - Aug-31-2023, 12:27 AM
RE: textfile to customtkinter combobox - by deanhystad - Aug-30-2023, 09:01 PM
RE: textfile to customtkinter combobox - by janeik - Aug-31-2023, 12:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Importing python data to Textfile or CSV yanDvator 0 1,820 Aug-02-2020, 06:58 AM
Last Post: yanDvator
  Replace Line in Textfile Deadline 1 10,476 Nov-04-2019, 07:14 PM
Last Post: Larz60+
  Compare input() to textfile Trianne 4 3,614 Sep-29-2018, 02:32 PM
Last Post: gruntfutuk
  Write from URL with changing information to textfile wmc326 1 3,066 Jul-12-2017, 07:10 PM
Last Post: wavic
  Loop thru textfile, change 3 variables for every line herbertioz 11 9,346 Nov-10-2016, 04:56 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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