Tkinter如何在不使用字典的情况下制作动态选项菜单 [英] Tkinter how to make a dynamic optionmenu without using a dictionary

查看:170
本文介绍了Tkinter如何在不使用字典的情况下制作动态选项菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个问题,这有助于看到方法,但我想使用他们的方法,而不使用字典。

I found this question, which was helpful to see the methodology, but I want to use their method without using a dictionary.

基本上,在Python中,我试图使用Tkinter中的两个选项菜单,其中第一个将包含excel文档的选项卡,然后第二个将包含我的信息从第一个选项菜单中选择选项卡后,从其中一列中读取。由于我以前不想存储和读取每个标签,所以在选择标签后,只需调用函数来读取数据,而不是事先创建一个字典,效率似乎更高。有人知道这样做吗?

Basically, in Python I'm trying to use two optionmenu's from Tkinter where the first will contain the tabs of an excel document, and then the second will contain information that I've read from one of the columns once the tab has been selected from the first optionmenu. As I don't want to store and read every single tab prior, it seems much more efficient to just call the function to read the data once the tab was chosen rather than create a dictionary beforehand. Does someone know a way of doing this?

这是我到目前为止(excel_transfer是一个基于openpyxl的自己创建的库,如果你有进一步的问题,我是什么你可以随便问一下,否则为了这个问题的目的,似乎不需要这样做):

Here is what I have so far(excel_transfer is a self created library based off openpyxl- if you have further questions on what I'm doing feel free to ask, otherwise it doesn't seem necessary to go into it for the purposes of this question):

import Tkinter
from Tkinter import *
from ttk import Frame
import manipulate_excel
import openpyxl
from openpyxl import load_workbook

class GUI(Frame):
    def read_tabs(self):
        wkbk = load_workbook('file.xlsx')
        sheets=wkbk.get_sheet_names()
        return sheets
    def read_columns(self):
        sheet = self.tabs_var.get()
        #if no value is selected, keep return a blank value
        if(sheet == ''):
            return ['']
        else:
            filepath = 'file.xlsx'
            workbook = excel_transfer.workbook(filepath)
            wb = workbook.open_existing_workbook()
            ws = wb(sheet)
            #step through the rows until a blank value is read
            row = 1
            current_row = 0
            previous_row = 0
            values = []
            #read the column, and append values to the array
            while current_row != None:
                previous_row = current_row
                row = row + 1
                cell = 'A' + str(row)
                current_row = workbook.read_new_cell(cell, ws)
                if(current_row != None):
                    values.append(current_row)
            #if there are no values, still return a single array value so it doesn't barf
            if(values== []):
                values= ['']
            return values

    def build_gui(self):
        n = Notebook(self)
        tab = Tkinter.LabelFrame(self, text='Tab')
        n.add(tab, text='Tab')
        n.pack()
        self.tabsvar = StringVar()
        self.tabsvar.set('')
        self.valuesvar = StringVar()
        self.valuesvar.set('')
        self.list_of_tabs = self.read_tabs()
        self.list_of_values = self.read_columns()
        self.TAB_OPTION = OptionMenu(tab, self.tabsvar, *self.list_of_tabs)
        self.TAB_OPTION.grid(row=0, column=0)
        self.VALUES_OPTION = OptionMenu(tab, self.valuesvar, *self.list_of_values)
        self.VALUES_OPTION.grid(row=1, column=0)

if __name__ == '__main__':
    root = Tk()
    app = GUI(root)
    root.mainloop()

第一个选项用于查找选项卡,但是我遇到的问题是只有在初始打开GUI时,read_column才会发生,它仍然是空白值。如何使self.list_of_values响应于在第一个选项菜单中选择哪个标签?我可以使用没有字典的痕迹吗?我相信有更好的方式编写我的代码 - 我开放的建设性的批评,因为总是有很多的学习。谢谢你的帮助!

The first option works to find the tabs, but the problem I have is the read_column only happens when the GUI is initially opened, it remains a blank value. How would I make the self.list_of_values responsive to which tab is chosen in the first optionmenu? Can I use trace without a dictionary? I'm sure there's prettier ways of writing the code I did- I'm open to constructive criticism as there's always plenty to learn. Thank you for the help!

推荐答案

好的,所以我找到答案,继续使用它来帮助任何人在未来有同样的问题。
我将self.TAB_OPTION更改为:

Ok, so I found the answer after continuing to work with it to help out anyone in future having the same problem. I changed self.TAB_OPTION to:

self.TAB_OPTION(tab, self.tabsvar, *self.list_of_tabs, command=self.read_columns)

通过添加这个,我不得不将一个变量传入self.read_columns ,因为添加命令会自动传递用户在第一个选项菜单中选择的值。所以,我把self.read_columns改成了:

By adding this, I had to pass in a variable into self.read_columns, as adding the command automatically passes the value the user selected in the first optionmenu. So, I changed self.read_columns to:

def read_columns(self, board):
    sheet = board
    #sheet = self.tabs_var.get()

我也拿出了显示为所以它收到了在第一个选项菜单中从TAB_OPTION中选择的值,而不是读取从GUI中选择的值。我还添加了:

I also took out the line shown to be commented above, so it received the value that was selected in the first optionmenu from the TAB_OPTION rather than reading what is selected from the GUI. I also added in:

self.VALUES_OPTION['menu'].delete(0, 'end')
for item in values:
    self.VALUES_OPTION['menu'].add_command(label=item)

在read_columns函数的末尾,它将重新生成第二个选项菜单的值。它删除所有先前的值,然后通过遍历数组来添加从excel读取的每个值。在函数结束时,我不再需要返回值。
我希望这有助于其他困惑的编码人员:)

at the end of the read_columns function so it would regenerate the values for the second optionmenu. It deletes all of the previous values, then adds each value that had been read from the excel in by iterating through the array. I no longer needed the return value at the end of the function. I hope this helps other confused coders :)

这篇关于Tkinter如何在不使用字典的情况下制作动态选项菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆