1

This is a practice project to help me learn python so it's all experimental. I've tried a variety of things and now I'm beginning to get lost in the haze (as you can probably see below). How can I populate a TKinter combobox from a simple txt file in Python 3? Any help would be great.

  • This program randomly generates 10 passwords which the user can select from and it will eventually be saved to a file with a date.
  • My text file is called PWFile.txt
  • PWFile.txt contains a set of 10 passwords in a column/list

The following does everything except populate the combobox:

In_file = open("PWfile.txt","r")
        line = f.readlines()
        print(line, end=' ')
        combovalues = []
        in_line = in_file.readlines()
        root = Tk() 
        root.title("Password GUI") 
        root.geometry('350x200') 
        Label(root, text="Password Options - Choose a password").grid(row=0)  
        combo = Combobox(root, width=28, height=10, font=("Helvetica", 9), state="enabled")
        combovalues.append(line)
        combo['text'] = combovalues
        combo.grid(column=0, row=1)
        Quitbtn = Button(root, text="Quit", command=quit) 
        Quitbtn.grid(column=2, row=1)
        root.mainloop()
Lazzo
  • 31
  • 1
  • 4
  • Any suggestions for reading data from a text file into a TKinter combobox? I've found some examples that I've tried but nothing has worked so far. Any suggestions would be appreciated. Thanks – Lazzo Jun 05 '18 at 14:14

1 Answers1

1

thats how I do it :

#First you create your function
def function_Name():

    var1 = open("c:\\your txt file path ", "r").readlines()

     data = []

     for line in var1:
        data.append(line)

      return data

#the you add to your combobox
combobox_name['values'] = function_Name()
PedroMiotti
  • 995
  • 10
  • 13