Python 如何获取组合框值

Python 如何获取组合框值,python,combobox,tkinter,ttk,Python,Combobox,Tkinter,Ttk,大家好,这是我在这里的第一篇帖子,我一直在阅读与这个主题相关的问题,但目前似乎没有任何效果,所以这里是我的问题。我创建了一个小应用程序来存储与我所做工作相关的数据。根据下面的代码,组合框如何不使用get()返回值` update_news_combo = None news = ['Mid Day News','Evening News'] features = ['Calling Farmers','Round About Ja','You and the Law','Get the Fa

大家好,这是我在这里的第一篇帖子,我一直在阅读与这个主题相关的问题,但目前似乎没有任何效果,所以这里是我的问题。我创建了一个小应用程序来存储与我所做工作相关的数据。根据下面的代码,组合框如何不使用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()
update\u news\u combo=None
新闻=[“中午新闻”,“晚间新闻”]
功能=[“打电话给农民”,“关于Ja的周围”,“你和法律”,“了解事实”,“职业谈话”,“经济和你”,“艺术版”,
《旅游综述》,《吉普》,《牙买加承诺》,《家庭事务》,《牙买加家庭周刊》,《城市透视》,《体育聚光灯》,
“分享爱”、“周六新闻”、“周日新闻”、“健康热线”、“开放专题”]
features.sort()
类菜单命令(对象):
def关于_弹出窗口(自我):
messagebox.showinfo(title=“关于功能跟踪器”,message='此应用程序用于跟踪功能和编辑的新闻')
def更新_弹出窗口(自):
messagebox.showinfo(title=“File Update”,message=“%s已添加”%Update\u news\u combo.get())
root=Tk()
root.title('功能跟踪')
根几何体('255x425')
updatename=ttk.Frame(根,填充=(5,10))
popup=MenuCommands()
#更新菜单框
updatename=ttk.Frame(根,填充=(5,10))
ttk.Label(updatename,text='Select Feature to add').grid(行=0,列=0)
更新功能组合=ttk.Combobox(更新名称,值=功能)
更新功能组合。绑定(“”)
更新功能组合网格(行=1,列=0)
ttk.Button(updateName,text='Add').grid(行=2,列=0)
ttk.Label(updatename,text='Select News to add').grid(行=3,列=0)
update\u news\u combo=ttk.Combobox(updatename,values=news)
update\u news\u combo.bind(“”)
更新\u news\u combo.grid(行=4,列=0)
news\u label=ttk.label(updatename,textvariable=update\u news\u combo.get())
新闻标签网格(行=6,列=0)
add_news=ttk.按钮(updatename,text='add',command=popup.update_popup)
添加_news.grid(行=5,列=0)
def show_view_frame():
viewframe.grid(行=0,列=0)
updatename.grid_忘记()
def show_update_frame():
网格(行=0,列=0)
viewframe.grid_忘记()
#带有菜单选项的菜单栏
菜单栏=菜单(根)
#更新菜单
filemenu=Menu(menubar,tearoff=0)
添加命令(标签='New',命令=显示更新框架)
文件菜单.add_命令(标签='View',命令=show_View_frame)
菜单栏.add_级联(标签='Update',菜单=文件菜单)
root.mainloop()
`
提前感谢您的建议和帮助。

这是一段代码,向您展示了如何使用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()
def foo(事件):#触发“”事件时调用的函数
打印v.get()#如何访问combobox所选项目
root=Tk()
v=StringVar()#用于保存用户选择的字符串变量
选项=[“选项1”、“选项2”、“选项3”]#可用组合框选项
帧=帧(根)
frame.pack()
combo=Combobox(根,textvariable=v,values=options)
combo.bind(“”,foo)#使用自定义回调绑定用户选择
组合。当前(1)#设置为默认“选项2”
combo.pack()
root.mainloop()
希望这有帮助


关于

我简化了代码并做了一个工作示例,但是在处理复杂的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()
从tkinter导入消息框,Tk,菜单,ttk
新闻=['中午新闻','晚间新闻']
功能=[‘打电话给农民’、‘围着Ja转’、‘你和法律’、‘了解事实’,
‘职业讲座’、‘经济与你’、‘艺术版’、‘旅游综述’,
“吉普”、“牙买加承诺”、“房屋事务”、“牙买加房屋周刊”]
features.sort()
类自定义菜单(对象):
def uuu init uuuuu(self,root,values=[]、组合位置=(0,0)、按钮位置=(0,0)、标签位置=(0,0)):
self.frame=根
self.combobox=ttk.combobox(self.frame,value=value)
self.combobox.bind(“”)
self.combobox.grid(行=组合位置[0],列=组合位置[1])
self.label=ttk.label(self.frame,textvariable=self.combobox.get())
self.label.grid(行=标签放置[0],列=标签放置[1])
self.button=ttk.button(self.frame,text=“Add”,command=self.update\u弹出窗口)
self.button.grid(行=按钮放置[0],列=按钮放置[1])
def更新_弹出窗口(自):
messagebox.showinfo(
title=“文件更新”,
message=“{}已添加”。格式(self.combobox.get())
)
root=Tk()
root.title('功能跟踪')
根几何体('255x425')
update_frame=ttk.frame(根,填充=(5,10))
def show_update_frame():
更新框架网格(行=0,列=0)
#更新菜单框
features_frame=CustomMenu(更新_frame,features,(1,0)、(3,0)、(0,0))
新闻帧=自定义菜单(更新帧,新闻,(4,0)、(5,0)、(6,0))
#带有菜单选项的菜单栏
菜单栏=菜单(根)
#更新菜单
filemenu=Menu(menubar,tearoff=0)
filemenu.add_命令(label='New',command=show_update_frame)
菜单栏.add_级联(label='Update',menu=filemenu)
root.config(menu=menubar)
root.mainloop()

这表明,如果可以不使用global,就不应该使用global。我创建的类非常难看,但至少每个“CustomMenu”都可以引用自己的组合框来获取所选值。

Welcome Stack Overflow。你能把你的代码减少到一个能展示问题的最小版本吗?我做了一些编辑,这足够了吗?还是我需要做更多?谢谢你的帮助,它成功地解决了我的问题。我也看到了你所做的,你创建了对象并在使用它们之前放置它们,这样你就不会在访问它们时遇到麻烦。
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()