tkinter和函数赋值中的循环按钮

时间:2017-03-22 12:33:31

标签: python excel tkinter

我已经能够使用此代码在tkinter中生成按钮

for i in range(0, num_sheets):
            an_sheet = ttk.Button(self, text = "%s" % sh_names[i], command = partial(load_sheets))
            an_sheet.grid(row = 1, column = i+1, sticky='w', pady = 10, padx = 10)

现在,这些按钮是根据Excel工作表中的工作表数生成的,并且还分配了它们所代表的工作表的名称。但是,有一个功能可以打印所代表的每张纸的内容。

def load_sheets():
            for i,sheetname in enumerate(sh_names) :
                xl_sheet = wb.sheet_by_name(sh_names[i])
                print()
                print(sheetname)
                row = xl_sheet.row(0)   
                for idx, cell_obj in enumerate(row):
                    cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')

                row = xl_sheet.nrows
                for col_idx in range(0, xl_sheet.ncols):
                    print ('Column: %s' % col_idx)
                    for row_idx in range(0, row):
                        cell_obj = xl_sheet.cell(row_idx, col_idx)
                        print ('Row: [%s] cell_obj: [%s]' % (row_idx, cell_obj))

现在的挑战是将函数绑定到按钮,以便在单击时打印出自己的内容。例如,当单击名为“工作表1”的按钮时,应打印工作表1的内容。

这是代码的完整结构。

class MainMenu(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)


        fname = join(dirname(dirname(abspath('C:/Users/qanda/OneDrive/Documents/Python Scripts/PEN'))), 'Python Scripts/PEN', 'Book1.xlsx')   
        wb = xlrd.open_workbook(fname)
        sh_names = wb.sheet_names()
        num_sheets = len(sh_names)

        def load_sheets():
            for i,sheetname in enumerate(sh_names) :
                xl_sheet = wb.sheet_by_name(sh_names[i])
                print()
                print(sheetname)
                row = xl_sheet.row(0)   
                for idx, cell_obj in enumerate(row):
                    cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')

                row = xl_sheet.nrows
                for col_idx in range(0, xl_sheet.ncols):
                    print ('Column: %s' % col_idx)
                    for row_idx in range(0, row):
                        cell_obj = xl_sheet.cell(row_idx, col_idx)
                        print ('Row: [%s] cell_obj: [%s]' % (row_idx, cell_obj))


        for i in range(0, num_sheets):
            an_sheet = ttk.Button(self, text = "%s" % sh_names[i], command = partial(load_sheets))
            an_sheet.grid(row = 1, column = i+1, sticky='w', pady = 10, padx = 10)

2 个答案:

答案 0 :(得分:1)

定义一个函数来打印一张纸的内容,将纸张编号作为参数。然后,使用functools.partial将命令与每个按钮相关联:

def print_sheet(sheet_number):
    ...

for i in range(0, num_sheets):
    an_sheet = ttk.Button(..., command = partial(print_sheet, i))
    ...

这是一个在循环中创建按钮的简短但完整的示例:

import tkinter as tk
from tkinter import ttk
from functools import partial

root = tk.Tk()

def print_sheet(i):
    print("printing sheet %d" % i)

for i in range(0, 5):
    button = ttk.Button(root, text="Button %d" % i,
                        command=partial(print_sheet, i))
    button.pack()

root.mainloop()

答案 1 :(得分:0)

所以你问的是如何迭代一组函数并分配它们?

你可以使用字典

我的A-level计算课程的例子

self.screen_info = {"Email Customer":  {"button_names": ["Enter Email"],         "commands": [self.email_from_email]},
                    "Text Customer":   {"button_names": ["Enter Number"],        "commands": [self.text_from_number]},
                    "Backup Database": {"button_names": ["Choose Location"],     "commands": [backup_database.backup]},
                    "Export Database": {"button_names": ["As .txt", "As .csv"],  "commands": [lambda: export.main("txt"), lambda: export.main("csv")]}}


def populate(self):
    for key in self.screen_info.keys():

        self.screen_info[key]["parent"] = tk.Frame(self.screens, width = 300, height = 300, bg = "#E6E6FA")
        self.screen_info[key]["parent"].place(relx = 0.5, rely = 0.5, anchor = tk.CENTER)

        for num in range(len(self.screen_info[key]["button_names"])):
            self.screen_info[key]["button_names"][num] = tk.Button(self.screen_info[key]["parent"],
                                                                   width = 20,
                                                                   text = self.screen_info[key]["button_names"][num],
                                                                   font = ("Ariel", 11, "bold"),
                                                                   command = self.screen_info[key]["commands"][num])
            self.screen_info[key]["button_names"][num].place(relx = 0.5, y = 60 + num * 40, anchor = tk.CENTER)

因此,这将遍历字典创建所有内容并分配命令。我使用lambda来导出数据库'因为函数需要一个参数,如果我没有使用lambda,那么函数会在程序启动后立即运行

编辑我使用关联的小部件覆盖字典中的每个键值,但如果您不再次引用它们,您甚至不需要设置变量,键入它们< / p>