Tkinter:如何使一个按钮与多个文本字段?



我在tkinter中有这个表。但是,我想在每行中只有一个带有四个文本字段的按钮。(我现在每行有四个按钮)

基本上,我想将这四个按钮连接到一行中的一个按钮。我该怎么做呢?
from tkinter import *
class Table:

def __init__(self,root):

# code for creating table
for i in range(total_rows):
for j in range(total_columns):

self.e = Button(root, width=20,text = lst[i][j])

self.e.grid(row=i, column=j)
# take the data
lst = [(1,'Raj','Mumbai',19),
(2,'Aaryan','Pune',18),
(3,'Vaishnavi','Mumbai',20),
(4,'Rachna','Mumbai',21),
(5,'Shubham','Delhi',21)]
# find total number of rows and
# columns in list
total_rows = len(lst)
total_columns = len(lst[0])
# create root window
root = Tk()
t = Table(root)
root.mainloop()

我已经修改了你的代码,包括一个名为'工作'的Toplevel窗口,可以访问超级按钮。按钮的数量由表中的行数决定。

每个按钮连接到另一个Toplevel窗口称为'edit'。

'Edit'包含Label,EntryButton对象。数量由表中的列数决定。

所有窗口都是可调整大小的,'工作'和'编辑'窗口可通过F1键访问。

转义键将通过messagebox关闭'top'表。

好的,这是最新的版本。

我把这个项目分成了两个python文件,一个是名为"dataffile .py"的数据文件,另一个是名为"Records.py"的代码文件。

'Datafile.py'必须与'Records.py. '分开保存。

记录代码完全可以通过更改Datafile.py

中的表数据来配置。通过添加或修改列和/或行来更改表数据将配置主程序。表、工作和编辑窗口由dataffile .py

中的行和列决定。另存为'Datafile.py'

import time
timedate = time.localtime()
year = timedate.tm_year
indice = [ "gender", "name", "location", "age", "birth"]
table = [
["Male",   "Raj",      "Mumbai",   19, year-19],
["Male",   "Aaryan",   "Pune",     18, year-18],
["Female", "Vaishnavi","Mumbai",   20, year-20],
["Female", "Rachna",   "Mumbai",   21, year-21],
["Male",   "Shubham",  "Delhi",    21, year-21],
["Male",   "Roger",    "New York", 17, year-17],
["Male",   "David",    "Sydney",   53, year-53],
["Female", "Jennifer", "London",   42, year-42]]

这是主程序'Records.py'。

另存为"Records.py">

import tkinter as tk
from tkinter import messagebox
from Datafile import timedate, indice, table
class Table:
def __init__(self, master):
self.master = root
self.master.title("Table")
# create table of buttons in self.store
self.rows = len(table)
self.columns = len(table[0])
# number of rows and columns
self.store = []
for r in range(self.rows):
self.store.append([])
for c in range(self.columns):
e = tk.Label(
self.master, width = 20, bd = 1,
relief = "raised", text = table[r][c])
e.grid(row = r, column = c, sticky = tk.NSEW)
self.flexx(self.master, r = r, c = c)
self.store[r].append(e) # all table buttons stored in list
self.master.geometry(f"{self.columns*100}x{self.rows*26}")
self.master.update()
# create toplevel widget for buttons
self.work = tk.Toplevel(self.master)
self.work.transient(self.master)
self.work.withdraw()
self.work.title("Individual Selector")
# populate toplevel widget with the Super buttons
# make buttons resizable
for r in range(self.rows):
e = tk.Button(
self.work, text = f"{table[r][1]}", bd = 1,
command = self.redirect(self.access, r))
e.grid(row=r, column=0, sticky = tk.NSEW)
self.flexx(self.work, r = r)
# Table edit window
self.edit = tk.Toplevel(self.master)
self.edit.transient(self.master)
self.edit.withdraw()
self.edit.title("Table Editor")
self.labelframe = tk.LabelFrame(self.edit, labelanchor = tk.N, text = "Name")
self.labelframe.grid(row = 0, column = 0, sticky = tk.NSEW)
self.flexx(self.edit)
# place entry tools here
self.data = []
for c in range(self.columns):
l = tk.Label(self.labelframe, text = f"{indice[c]}")
l.grid(row = c, column = 0, sticky = tk.NSEW)
e = tk.Entry(self.labelframe)
e.grid(row = c, column = 1, sticky = tk.NSEW)
self.data.append(e)
b = tk.Button(
self.labelframe, text = "Enter", bd = 1,
command = self.redirect(self.change, c))
b.grid(row = c, column = 2, sticky = tk.NSEW)
self.flexx(self.labelframe, r = c, c = 1)
# wire up master and work widgets
for a, b in [(self.master, self.closer),
(self.work, self.opener),
(self.edit, self.editor)]:
a.protocol('WM_DELETE_WINDOW', b)
a.bind("<KeyRelease-Escape>", b)
# Keys to work widget
self.master.bind("<F1>", self.opener)
self.work.bind("<F1>", self.opener)
self.edit.bind("<F1>", self.editor)
def redirect(self, f, *args):
return lambda: f(*args)
def flexx(self, m, r = 0, c = 0, rw = 1, cw = 1):
m.rowconfigure( r, weight = rw )
m.columnconfigure( c, weight = cw )
def change(self, c):
e = self.data[c]
self.store[self.currentRow][c]["text"] = e.get()
table[self.currentRow][c] = e.get()
def closer(self, ev = None):
if messagebox.askquestion(title = "Quit", message = "Confirm",
detail = "Close program?") == "yes":
self.master.destroy()
def opens( self, o, m ):
"""Toggle switch for toplevel"""
if o.winfo_viewable( ):
o.withdraw( )
m.focus_set( )
else:
o.focus_set( )
o.deiconify()
def position(self, o):
x = int(o.winfo_x() + o.winfo_width() + 5)
y = int(o.winfo_y() + o.winfo_height() / 2)
return x, y
def opener(self, ev = None):
x, y = self.position( self.master)
self.work.geometry(f"190x{26*self.rows}+{x}+{y}")        
self.opens(self.work, self.master)
def editor(self, ev = None):
x, y = self.position( self.work)
self.edit.geometry(f"213x{26*self.columns}+{x}+{y}")
self.opens(self.edit, self.work)
def access(self, *args):
self.currentRow = r = args[0]
self.labelframe["text"] = f"{table[r][1]}"
for i,e in enumerate(self.data):
e.delete(0, "end")
e.insert(0, table[r][i])
self.editor()
if __name__ == "__main__":
root = tk.Tk()
top = Table(root)
root.mainloop()

相关内容

  • 没有找到相关文章

最新更新