如何将一个tkinter组合框小部件添加到也有文本输入小部件的gui?(How can I add a tkinter combobox widget to a gui that also has t

编程入门 行业动态 更新时间:2024-10-25 17:15:52
如何将一个tkinter组合框小部件添加到也有文本输入小部件的gui?(How can I add a tkinter combobox widget to a gui that also has text entry widgets?)

我正在开发一个带有文本输入小部件的gui,它可以根据表中的记录数重新生成。 我正在使用一个字段列表,每个字段都创建为一个条目小部件。 对于这些字段中的至少一个,我想设置一个组合框,其值可供用户选择。 我一直在玩添加一个组合框到根(我刚刚插入了一个样本)但是当我运行脚本时它没有出现。 我没有收到错误,gui显示所有输入框,但不显示组合框。 有没有人有任何想法:以下是一些代码:

import arcpy, tkMessageBox, ctypes, ttk from Tkinter import * mosaicD = r'C:\temp\temp.gdb\mapIndex_MD_test' mapIndexTbl = r'C:\temp\temp.gdb\mapIndexTestTable' formFields = ['County', 'Route', 'PMB', 'PME', 'Map_Sheet_Desc', 'HangingFileGroup', 'MapSubSet', 'MapSubSetStatus', 'Shtext', 'Status', 'SubStatus', 'MapDatum', 'Geo_Referenced_Datum', 'MapScale', 'CAD', 'DrawingDate', 'FileExtention','Original_MrSID_Filename'] fields = arcpy.ListFields(mapIndexTbl) with arcpy.da.SearchCursor(mosaicD,'name') as cursorB: for rowB in cursorB: inputValues = [] def fetch(entries): for entry in entries: field = entry[0] text = entry[1].get() inputValues.append(text) root.destroy() def makeform(root, fields): entries = [] for field in fields: row = Frame(root) lab = Label(row, width=20, text=field, anchor='w') ent = Entry(row) row.pack(side=TOP, fill=X, padx=5, pady=5) lab.pack(side=LEFT) ent.pack(side=RIGHT, expand=YES, fill=X) entries.append((field, ent)) return entries if __name__ == '__main__': root = Tk() root.iconbitmap(r'\\sv04docifs5\data5\vol2\Records\GIS\Logos\CT.ico') root.geometry('375x650') root.wm_title('Enter values for '+rowB[0]) cities = ('Toronto', 'Ottawa', 'Montreal', 'Vancouver', 'St. John') cblist = Frame(root) cbp3 = ttk.Labelframe(cblist, text='Pre-defined List') cb3 = ttk.Combobox(cbp3, values=cities, state='readonly') cb3.current(1) # set selection cb3.pack(side=LEFT, expand=YES, fill=X) # position and display cbp3.pack(in_=cblist, side=TOP, pady=5, padx=10) ents = makeform(root, formFields) root.bind('<Return>', (lambda event, e=ents: fetch(e))) b1 = Button(root, text='Submit', command=(lambda e=ents: fetch(e))) b1.pack(padx=5, pady=5,anchor=CENTER) #b2 = Button(root, text='Quit', command=root.quit) #b2.pack(side=LEFT, padx=5, pady=5) root.mainloop()

I am developing a gui with text entry widgets that regenerate based on the number of a records in a table. I am using a list of fields that are each created as an entry widget. For at least one of these fields, I want to set up a combobox with values for the user to choose from. I've been playing around with adding a combobox to the root (I've just inserted a sample one for now) but it doesn't show up when I run the script. I don't get an error, the gui shows up with all the entry boxes, but not the combobox. Does anyone have any ideas: Here is some of the code:

import arcpy, tkMessageBox, ctypes, ttk from Tkinter import * mosaicD = r'C:\temp\temp.gdb\mapIndex_MD_test' mapIndexTbl = r'C:\temp\temp.gdb\mapIndexTestTable' formFields = ['County', 'Route', 'PMB', 'PME', 'Map_Sheet_Desc', 'HangingFileGroup', 'MapSubSet', 'MapSubSetStatus', 'Shtext', 'Status', 'SubStatus', 'MapDatum', 'Geo_Referenced_Datum', 'MapScale', 'CAD', 'DrawingDate', 'FileExtention','Original_MrSID_Filename'] fields = arcpy.ListFields(mapIndexTbl) with arcpy.da.SearchCursor(mosaicD,'name') as cursorB: for rowB in cursorB: inputValues = [] def fetch(entries): for entry in entries: field = entry[0] text = entry[1].get() inputValues.append(text) root.destroy() def makeform(root, fields): entries = [] for field in fields: row = Frame(root) lab = Label(row, width=20, text=field, anchor='w') ent = Entry(row) row.pack(side=TOP, fill=X, padx=5, pady=5) lab.pack(side=LEFT) ent.pack(side=RIGHT, expand=YES, fill=X) entries.append((field, ent)) return entries if __name__ == '__main__': root = Tk() root.iconbitmap(r'\\sv04docifs5\data5\vol2\Records\GIS\Logos\CT.ico') root.geometry('375x650') root.wm_title('Enter values for '+rowB[0]) cities = ('Toronto', 'Ottawa', 'Montreal', 'Vancouver', 'St. John') cblist = Frame(root) cbp3 = ttk.Labelframe(cblist, text='Pre-defined List') cb3 = ttk.Combobox(cbp3, values=cities, state='readonly') cb3.current(1) # set selection cb3.pack(side=LEFT, expand=YES, fill=X) # position and display cbp3.pack(in_=cblist, side=TOP, pady=5, padx=10) ents = makeform(root, formFields) root.bind('<Return>', (lambda event, e=ents: fetch(e))) b1 = Button(root, text='Submit', command=(lambda e=ents: fetch(e))) b1.pack(padx=5, pady=5,anchor=CENTER) #b2 = Button(root, text='Quit', command=root.quit) #b2.pack(side=LEFT, padx=5, pady=5) root.mainloop()

最满意答案

组合框cb3包装在框架cbp3 。 该框架包含在框架cblist 。 无处在cblist上调用pack或grid 。 由于cblist是隐形的,它的孩子也是如此。

The combobox cb3 is packed in the frame cbp3. That frame is packed in frame cblist. Nowhere do you call pack or grid on cblist. Since cblist is invisible, so are its children.

更多推荐

本文发布于:2023-07-05 01:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   也有   组合   如何将   文本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!