tkinter中的循环按钮和功能分配 [英] Looping Buttons in tkinter and function assignment
问题描述
我已经能够使用此代码在tkinter中生成Buttons
i have been able to generate Buttons in tkinter using this code
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工作表中工作表的数量生成的,并且还被分配了它们代表的工作表的名称.但是,有一个功能可以打印所代表的每张纸的内容.
now these buttons have been generated based on the number of sheets in an excel sheet and have also been assigned the names of the sheets they represent. However, there's a function to print the contents of each sheet represented.
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的内容.
now the challenge is to bind the function to the button such that it prints out it's own content when it's clicked. For example, when a button named 'sheet 1' is clicked, the contents of sheet 1 should be printed.
这是代码的完整结构.
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)
推荐答案
定义一个函数,以工作表编号作为参数打印一张工作表的内容.然后,使用functools.partial将命令与每个按钮关联:
Define a function to print the contents of one sheet, taking the sheet number as an argument. Then, use functools.partial to associate a command with each button:
def print_sheet(sheet_number):
...
for i in range(0, num_sheets):
an_sheet = ttk.Button(..., command = partial(print_sheet, i))
...
这是在循环中创建按钮的简短但完整的示例:
Here's a short but complete example of creating buttons in a loop:
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()
这篇关于tkinter中的循环按钮和功能分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!