大家好,这是我在这里发表的第一篇文章,我一直在阅读与此主题相关的问题,但目前似乎没有任何工作,所以这是我的问题。我创建了一个小应用程序来存储与我所做工作相关的数据。怎么组合框没有使用get()返回值,根据下面的代码提出任何建议?`
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()
` 提前感谢您的建议和帮助。
答案 0 :(得分:3)
这是一段代码,向您展示如何使用ttk Combobox小部件:
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()
希望这有帮助。
此致
答案 1 :(得分:1)
我简化了你的代码并制作了一个工作示例,但在处理复杂的GUI之前,你应该考虑更多地学习Python。
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()
显示的是,如果没有它们,就不应该使用全局。我制作的课程非常难看,但至少每个“CustomMenu”都可以引用自己的组合框来获取所选的值。
答案 2 :(得分:1)
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()