Editable Combobox in Python - python

How can I set the List as the values for the Combobox numberChosen? After this, I want to edit the List with my entries. Do I need a loop for this?
It would be great if somebody would help me, thank you!
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
# window
win = tk.Tk()
win.title("menu")
# button click event
def clickMe():
action.configure(command='List = [nameEntered]')
# text box entry
ttk.Label(win, text="Eingabe:").grid(column=0, row=0)
name = tk.StringVar()
nameEntered = ttk.Entry(win, width=12, textvariable=name)
nameEntered.grid(column=0, row=1)
# button
action = ttk.Button(win, text="Enter", command=clickMe)
action.grid(column=2, row=1)
List = [nameEntered]
# drop down menu
ttk.Label(win, text="Auswahl:").grid(column=1, row=0)
number = tk.StringVar()
numberChosen = ttk.Combobox(win, width=12)
numberChosen['values'] = List
numberChosen.grid(column=1, row=1)
win.mainloop()

You need to reconfigure the combobox whenever you want to add an item to the list.
Example:
def clickMe():
List.append(name.get())
numberChosen.configure(values=List)

Related

Tkinter messagedialog askyesno with a checkbox?

Is it possible to make a Tkinter yes/no messagebox with a checkbox, for something like 'Never ask me again'?
Or would I have to create another window, create my own Labels and CheckButtons, and basically create my own dialog?
You should create your own dialog box then.
Here’s what you can do:
from tkinter import *
def popup():
popupWin = Toplevel()
popupWin.title(“Continue?”)
checkVariable = IntVar()
lbl = Label(popupWin, text=“Continue?)
lbl.pack()
btn2 = Button(popupWin, text=“Yes”)
btn2.pack()
btn3 = Button(popupWin, text=“No”)
btn3.pack()
checkBox = Checkbutton(popupWin, text=“Don’t ask again”, variable=checkVariable)
root = Tk()
btn = Button(root, text=Message Box, command=popup)
root.mainloop()

How To Repeatedly Delete Differently Named Items

Recently I was working on a program where when one clicked a button, it would delete all of the tkinter buttons they made through a .yml file. Here is an example of what I mean:
(All TKinter Root Init Here)
button1 = Button(root, text="hi")
button2 = Button(root, text="hi again")
button3 = Button(root, text="hi again again")
button4 = Button(root, text="OK this is getting tiring")
button5 = Button(root, text="go away")
button6 = Button(root, text="...")
def del_all():
for i in range(999999999):
button(i).place_forget() #I was hoping to make button(i) give the output button1, then button2, and so on.
root.mainloop()
Try nametowidget in tkinter,example like:
import tkinter as tk
r = tk.Tk()
for i in range(5):
tk.Button(r,text=i).pack()
r.nametowidget(".!button").pack_forget()
r.mainloop()
This will remove the first button.If you want to remove the second button, you need to use r.nametowidget(".!button2").pack_forget()
So for you code,you may need to use:
def del_all():
root.nametowidget(".!button").place_forget()
for i in range(2, 999999999):
root.nametowidget(".!button"+str(i)).place_forget()
About the parameter in the nametowidget, there is a clear description.
You could also use winfo_children and use .widgetName to check whether it is a button,like:
import tkinter as tk
r = tk.Tk()
tk.Label(r, text="test").pack()
for i in range(5):
tk.Button(r,text=i).pack()
for i in r.winfo_children():
if i.widgetName == 'button':
i.pack_forget()
r.mainloop()
The solution would depend on how the buttons are named/stored.
For example, if the buttons were a list. Something like:
buttons = ['button1', 'button2', 'button3', 'button4']
Then you an delete by calling:
buttons.remove()
And that would 'clear' the list.

How to send information to a ScrolledText from a form in Tkinter with Python 3?

I made a form in Tkinter but when it is anwered I need the information to concatenate to a textbox.
Let's say I select I live in Chicago, how do I send that information to the scrolled text when I press the button "send"?
lblCd= Label(ventana,text="Location:",font= ("Arial", 20),
fg="blue", bg="white").place(x=70,y=400)
combo['values']=("Chicago","NY", "Texas")
combo.current(1)
combo.place(x=260, y=400)
Btn=Button(ventana, text="Send",)
Btn.place(x=200,y=500)
txt=scrolledtext.ScrolledText(ventana, width=40, height=10)
txt.place(x=260, y= 550)
txt.insert(INSERT, Nombre)
You can use Button(..., command=function_name) to execute function_name() when you press button. This function can get selected value and insert in scrolledtext.
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.scrolledtext as scrolledtext
def send_data():
txt.insert('end', combo.get() + '\n')
root = tk.Tk()
label = tk.Label(root, text="Location:")
label.pack()
combo = ttk.Combobox(root, values=("Chicago","NY", "Texas"))
combo.pack()
combo.current(0)
button = tk.Button(root, text="Send", command=send_data)
button.pack()
txt = scrolledtext.ScrolledText(root)
txt.pack()
root.mainloop()

How can I make a Tkinter OptionMenu not change the text when an option is selected?

I would like to make a file button on my Tkinter application that displays three of four options to the user. I have this code:
self.file_button_text = StringVar(master)
self.file_button_text.set('File')
self.file_buton = OptionMenu(self, self.file_button_text, "Com Ports", "Bottle Information", "Reset Graphs")
self.file_buton.grid(row=0, column=0)
self.file_button_text.trace("w", self.file_option)
def file_option(self, *args):
print(self.file_button_text.get())
self.file_button_text.set('File')
However, once an option is selected, the text of the button changes to that option. Is there a way I can get the value of the selection without changing the text of the button itself? I tried using trace to see the option selected and then change the text back to File but it takes too long. Is there a better/another way to do this?
An option menu is nothing but a Menubutton with a Menu, and some special code specifically to change the text of the button. If you don't need that feature, just create your own with a Menubutton and Menu.
Example:
import tkinter as tk
root = tk.Tk()
var = tk.StringVar()
label = tk.Label(root, textvariable=var)
menubutton = tk.Menubutton(root, text="Select an option",
borderwidth=1, relief="raised",
indicatoron=True)
menu = tk.Menu(menubutton, tearoff=False)
menubutton.configure(menu=menu)
menu.add_radiobutton(label="One", variable=var, value="One")
menu.add_radiobutton(label="Two", variable=var, value="Two")
menu.add_radiobutton(label="Three", variable=var, value="Three")
label.pack(side="bottom", fill="x")
menubutton.pack(side="top")
root.mainloop()

Python Tkinter GUI:add text from an entry widget in a pop up window to a listbox in a different window?

I am trying to add an entry from a toplevel window into a listbox in the main window.
So far I have managed to create a button that opens a new window containing 4 entry widgets(name, address, phone number and DOB). Is there any way, after I press the OK button on the pop up window, that all four entries are added to the listbox on the main window?
Thanks.
Unless I'm missing something in your problem description, the OK button command just needs to copy the values from the Entry fields to the Listbox. Was there more to it than that?
from tkinter import Tk, Frame, Label, Entry, Button, Listbox
def ok_button():
li.delete(0, "end")
for i in range(len(fields)):
li.insert("end", e[i].get())
root = Tk()
root.title("Listbox")
cf = Frame(root)
cf.pack()
fields = ("Name", "Address", "Phone", "DOB")
e = []
for f in fields:
i = len(e)
Label(cf, text=f).grid(column=2, row=i, sticky="e")
e.append(Entry(cf, width=16))
e[i].grid(column=4, row=i)
Button(cf, text="OK", command=ok_button).grid(column=2, row=10, columnspan=3)
li = Listbox(cf)
li.grid(column=2, row=8, columnspan=3)
root.mainloop()

Categories