仅作为背景知识,“ Student_Records”是我的代码的第4类,而“ search_by_roll_no”是我需要的“ Student_Records”类中输入字段内容的第5类。请提供解决方案...
没有尝试按对象访问值,而是尝试了几乎所有可以找到的东西。 下面只是给出错误的代码片段。
import tkinter as tk # python 3
from tkinter import font as tkfont # python 3
from tkinter import *
import sqlite3
import os
import sys
from PIL import Image, ImageTk
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='times new roman', size=24, weight="bold", slant="italic")
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, Student_Records, roll_no_search):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
frame.config(background='gainsboro')
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
def get_page(self, page_name):
for page in self.frames.values():
if str(page.__class__.__name__) == page_name:
return page
return None
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Student Records", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
label1 = tk.Label(self, text="Search", font=controller.title_font)
label1.pack(side="top", fill="x", pady=10)
button3 = tk.Button(self, text="Student Records",font=('times new roman',16,'bold'),
command=lambda: controller.show_frame("Student_Records"))
button3.pack(fill='x',pady=10)
class Student_Records(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller=controller
label = tk.Label(self,text="Search Student Records", font=controller.title_font)
label.pack(side="top", fill="x",pady=10)
b1=tk.Button(self, text="run",
command=lambda:self.make_widget(controller))
b1.pack()
def make_widget(self,controller):
label1=tk.Label(self,text="Roll No", font=('times new roman',14,'bold'))
label1.place(x=150,y=70)
some_input = "test input widget"
entry1=tk.Entry(self, textvariable=some_input, width=8)
entry1.pack(side="top",pady=10)
e1_var=tk.StringVar(entry1.get())
button1=tk.Button(self, text="Search By Roll No", font=('times new roman',16,'bold'),
command=lambda:controller.show_frame("roll_no_search"))
button1.pack(fill='x', side="top",pady=10)
class roll_no_search(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller=controller
label = tk.Label(self,text="Search Student Records", font=controller.title_font)
label.pack(side="top", fill="x",pady=10)
b2 = tk.Button(self, text = "print",
command = lambda: self.print_it())
b2.pack()
def print_it(self):
student_records = self.controller.get_page("Student_Records")
value = student_records.entry1.get()
print (value)
if __name__ == "__main__":
app = SampleApp()
app.title('Student Records')
app.geometry('600x600')
app.mainloop()
我收到的错误是: 'Student_Records'对象没有属性'entry1'