提问人:user83975 提问时间:3/23/2022 最后编辑:user83975 更新时间:3/23/2022 访问量:156
创建面向对象的 tkinter 程序:尝试将字符串值传递给在单独类中定义的方法
Creating an object oriented tkinter program: Trying to pass a string value to a method defined in a separate class
问:
我正在尝试创建一个面向对象的 tkinter 程序。截至目前,我已经写了 4 节课。类 1 是主程序,类 2 是主框架,类 3 是 AlumniSearch 框架,类 4 是 AlumniProfile 显示框架,其中该框架显示在名为“alumniProfile”的导入类中编译的所有信息。
我的目标是使用在“alumniSearch”类中选择的字符串值来更新/更改在“alumniProfiles”中创建的对象,以便在“alumniProfiles”页面上显示该对象。
import tkinter as tk
from tkinter import ttk
from personClass import alumniProfile
class alumniSearch(ttk.Frame):
def __init__(self, container, controller, **kwargs):
super().__init__(container, **kwargs)
self.searchBox = ttk.Entry(self)
self.searchBox.grid(row=1, column=1)
searchButton = ttk.Button(self, text='Go', command=self.searchAlumni)
searchButton.grid(row=1, column=2)
self.selectedResult = tk.StringVar()
self.resultsBox = tk.Listbox(self, listvariable=self.selectedResult, width=30)
self.resultsBox.pack(side='left', fill='x')
viewProfileButton = ttk.Button(self, text='View Alumni Profile', command=self.passName)
viewProfileButton.pack(side='left')
def passName(self):
selectedName = ''
for i in self.resultsBox.curselection():
selectedName = self.resultsBox.get(i)
# need something here to pass the selectedName to the self.alumniPerson object in alumniProfiles class
class alumniProfiles(ttk.Frame):
def __init__(self, container, controller, **kwargs):
super().__init__(container, **kwargs)
self.selectedName = 'Mr. Bryan B. Kornegay Jr.' # default name until another name is
# selected in alumniSearch class
self.alumniPerson = alumniProfile(self.selectedName) # builds object using the name given
personValues = self.alumniPerson.compileValues() # initializes the method to create the label
self.valuesVariable = tk.StringVar()
self.valuesVariable.set(personValues)
self.displayValues = ttk.Label(self, textvariable=self.valuesVariable)
self.displayValues.pack(side='left')
def setName(self, name): # needs to dynamically update the label displayed on the page
self.selectedName = name
self.valuesVariable.set(name)
return self.valuesVariable
答: 暂无答案
评论