Reputation: 13
Hi everyone this is my first post here, i have been reading the questions relating to this topic but nothing seems to work at the moment so here is my question. I created a small app to store data relating to the work i do. how ever the combo box is not returning the value using the get() any suggesting based on the code below?`
update_news_combo = None
news = ['Mid Day News','Evening News']
features = ['Calling Farmers','Round About Ja','You and the Law','Get the Facts','Career Talk', 'Economy and you','Arts Page',
'Tourism Roundup','Jeep','Jamaica Promise','House Matters','Jamaica House Weekly','Urbanscope','Sports Spotlight',
'Share the love','Saturday News','Sunday News','Healthline','Open Feature']
features.sort()
class MenuCommands(object):
def about_popup(self):
messagebox.showinfo(title = "About Feature Tracker", message = 'This app is used to track Features and news edited')
def update_popup(self):
messagebox.showinfo(title = "File Update", message = "%s has been Added"%update_news_combo.get())
root = Tk()
root.title('Feature Tracking')
root.geometry('255x425')
updateframe = ttk.Frame(root,padding = (5,10))
popup = MenuCommands()
#Update Menu Frame
updateframe = ttk.Frame(root,padding = (5,10))
ttk.Label(updateframe,text ='Select Feature to add').grid(row = 0,column = 0)
update_feature_combo = ttk.Combobox(updateframe,values=features)
update_feature_combo.bind("<<>ComboboxSelected>")
update_feature_combo.grid(row = 1,column = 0)
ttk.Button(updateframe,text ='Add').grid(row = 2, column = 0)
ttk.Label(updateframe,text ='Select News to add').grid(row = 3,column = 0)
update_news_combo = ttk.Combobox(updateframe,values=news)
update_news_combo.bind("<<>ComboboxSelected>")
update_news_combo.grid(row = 4, column = 0)
news_label = ttk.Label(updateframe,textvariable = update_news_combo.get())
news_label.grid(row = 6,column = 0)
add_news =ttk.Button(updateframe,text ='Add',command = popup.update_popup)
add_news.grid(row = 5, column = 0)
def show_view_frame():
viewframe.grid(row = 0, column = 0)
updateframe.grid_forget()
def show_update_frame():
updateframe.grid(row = 0, column = 0)
viewframe.grid_forget()
#Menu bar with menu options
menubar = Menu(root)
#Update Menu
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label = 'New',command =show_update_frame)
filemenu.add_command(label = 'View',command =show_view_frame)
menubar.add_cascade(label = 'Update',menu = filemenu)
root.mainloop()
` Thanks in advance for your suggestions and help.
Upvotes: 0
Views: 31480
Reputation: 49
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
root = Tk()
root.geometry("400x400")
#^ Length and width window :D
cmb = ttk.Combobox(root, width="10", values=("prova","ciao","come","stai"))
#^to create checkbox
#^cmb = Combobox
#now we create simple function to check what user select value from checkbox
def checkcmbo():
if cmb.get() == "prova":
messagebox.showinfo("What user choose", "you choose prova")
#^if user select prova show this message
elif cmb.get() == "ciao":
messagebox.showinfo("What user choose", "you choose ciao")
#^if user select ciao show this message
elif cmb.get() == "come":
messagebox.showinfo("What user choose", "you choose come")
elif cmb.get() == "stai":
messagebox.showinfo("What user choose", "you choose stai")
elif cmb.get() == "":
messagebox.showinfo("nothing to show!", "you have to be choose something")
cmb.place(relx="0.1",rely="0.1")
btn = ttk.Button(root, text="Get Value",command=checkcmbo)
btn.place(relx="0.5",rely="0.1")
root.mainloop()
Upvotes: 1
Reputation: 471
I simplified your code and made a working example but you should consider learning Python a bit more before dealing with complex GUI.
from tkinter import messagebox, Tk, Menu, ttk
news = ['Mid Day News', 'Evening News']
features = ['Calling Farmers', 'Round About Ja', 'You and the Law', 'Get the Facts',
'Career Talk', 'Economy and you', 'Arts Page', 'Tourism Roundup',
'Jeep','Jamaica Promise', 'House Matters', 'Jamaica House Weekly']
features.sort()
class CustomMenu(object):
def __init__(self, root, values=[], combo_placement=(0, 0), button_placement=(0, 0), label_placement=(0, 0)):
self.frame = root
self.combobox = ttk.Combobox(self.frame, values=values)
self.combobox.bind("<<>ComboboxSelected>")
self.combobox.grid(row=combo_placement[0], column=combo_placement[1])
self.label = ttk.Label(self.frame, textvariable=self.combobox.get())
self.label.grid(row=label_placement[0], column=label_placement[1])
self.button = ttk.Button(self.frame, text="Add", command=self.update_popup)
self.button.grid(row=button_placement[0], column=button_placement[1])
def update_popup(self):
messagebox.showinfo(
title="File update",
message="{} has been added".format(self.combobox.get())
)
root = Tk()
root.title('Feature Tracking')
root.geometry('255x425')
update_frame = ttk.Frame(root, padding=(5,10))
def show_update_frame():
update_frame.grid(row=0, column=0)
#Update Menu Frame
features_frame = CustomMenu(update_frame, features, (1, 0), (3, 0), (0, 0))
news_frame = CustomMenu(update_frame, news, (4, 0), (5, 0), (6, 0))
#Menu bar with menu options
menubar = Menu(root)
#Update Menu
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label='New', command=show_update_frame)
menubar.add_cascade(label='Update', menu=filemenu)
root.config(menu = menubar)
root.mainloop()
What that shows is that you should not use global if you can do without them. The class I made is pretty ugly but at least each of the "CustomMenu" can reference to its own combobox to fetch the selected value.
Upvotes: 2
Reputation: 728
this is a snippet of code that shows you how to use ttk Combobox widget:
def foo(event):#function called when '<<ComboboxSelected>>' event is triggered
print v.get()#how to access to combobox selected item
root = Tk()
v = StringVar()#a string variable to hold user selection
options=["option 1", "option 2", "option 3"] #available combobox options
frame = Frame(root)
frame.pack()
combo = Combobox(root,textvariable=v, values=options)
combo.bind('<<ComboboxSelected>>',foo)#binding of user selection with a custom callback
combo.current(1)#set as default "option 2"
combo.pack()
root.mainloop()
Hope this helps.
Regards
Upvotes: 4