一只燕子鸡 2022-12-24 00:23 采纳率: 75%
浏览 48
已结题

python函数类无法调用

请新建一个大文件然后每一个代码模块请新建一个存放

img

# 登陆模块
import tkinter as tk
from tkinter import messagebox
from ck import ck
from MianPage import MianPage


class LoginPage:

    def __init__(self, master):
        self.root = master
        self.root.geometry('300x180')
        self.root.title('登陆页面')

        self.username = tk.StringVar()
        self.password = tk.StringVar()

        self.page = tk.Frame(root)
        self.page.pack()

        tk.Label(self.page).grid(row=0, column=0)

        tk.Label(self.page, text='账户:').grid(row=1, column=1)
        tk.Entry(self.page, textvariable=self.username).grid(row=1, column=2)

        tk.Label(self.page, text='密码:').grid(row=2, column=1)
        tk.Entry(self.page, textvariable=self.password).grid(row=2, column=2, pady=10)

        tk.Button(self.page, text='登陆', command=self.login).grid(row=3, column=1, pady=10)
        tk.Button(self.page, text='退出', command=self.page.quit).grid(row=3, column=2)

    def login(self):
        name = self.username.get()
        pwd = self.password.get()
        flag, message = ck.check_login(name, pwd)
        if flag:
            print('登陆成功')
            self.page.destroy()
            MianPage(self.root)

        else:
            messagebox.showwarning(title='警告', message='登陆失败,请检查账号密码是否正确')


if __name__ == '__main__':
    root = tk.Tk()
    LoginPage(master=root)
    root.mainloop()


import json


# 以下是数据模块

class MysqlDate:
    def __init__(self):
        self.students = None
        self.users = json.loads(open('users.json', mode='r', encoding='utf-8').read())
        self.students = json.loads(open('students.json', mode='r', encoding='utf-8').read())

    def check_login(self, username, password):  # 登陆界面
        for user in self.users:
            if username == user['username']:
                if password == user['password']:
                    return True, '登陆成功'
                else:
                    return False, '登陆失败,密码错误'
        return False, '登陆失败,用户不存在'

    def all(self):
        return self.students

    def insert(self, student):  # 信息录入获取
        self.students.append(student)

    def delete_by_username(self, name):  # 根据姓名删除数据
        for student in self.students:
            print(1)
            if student['name'] == name:
                self.students.remove(student)
                return True, '用户删除成功'
            return False, f'{name}用户不存在'

    def search_by_username(self, name):  # 根据姓名查询py数据
        for student in self.students:
            if student['name'] == name:
                self.students.remove(student)
                return True, '用户删除成功'
            return False, f'{name}用户不存在'

    def update(self, stu):  # 判断需要修改的用户是否存在
        for student in self.students:
            if student['name'] == stu['name']:
                self.students.remove(student)
                student.update(stu)
                return True, f'{stu["name"]}用户修改数据成功'
            return False, f'{stu["name"]}用户不存在'


ck = MysqlDate()
if __name__ == '__main__':
    ck.check_login('admin', 'password')
    ck.all()



import tkinter as tk
from tkinter import ttk
from ck import ck
# 以下是页面逻辑模块


class AboutFrame(tk.Frame):
    def __init__(self, root):
        super().__init__(root)  # 关于的封装在此
        tk.Label(self, text='BY tkinter').pack()
        tk.Label(self, text='BY JK7B YZJ').pack()


class ChangeFrame(tk.Frame):    # 修改模块
    def __init__(self, root):
        super().__init__(root)  # 修改界面
        self.name = tk.StringVar()
        self.chinese = tk.StringVar()
        self.math = tk.StringVar()
        self.english = tk.StringVar()
        self.status = tk.StringVar()

    def create_page(self):  # 查询界面设计
        tk.Label(self).grid(row=1, pady=10)

        tk.Label(self, text='姓 名').grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.name).grid(row=1, column=2, pady=10)
        tk.Label(self, text='语 文').grid(row=2, column=1, pady=10)
        tk.Entry(self, textvariable=self.chinese).grid(row=2, column=2, pady=10)
        tk.Label(self, text='数 学').grid(row=3, column=1, pady=10)
        tk.Entry(self, textvariable=self.math).grid(row=3, column=2, pady=10)
        tk.Label(self, text='英 语').grid(row=4, column=1, pady=10)
        tk.Entry(self, textvariable=self.english).grid(row=4, column=2, pady=10)

        tk.Button(self, text='查询', command=self.search_user).grid(row=5, column=1, pady=10)
        tk.Button(self, text='修改', command=self.change_user).grid(row=5, column=2, pady=10)
        tk.Label(self, textvariable=self.status).grid(row=6, column=2, pady=10, sticky=tk.E)

    def search_user(self):  # 查询用户
        flag, info = ck.search_by_username(self.name.get())
        if flag:    # 数据判断及查询数据
            self.name.set(info['name'])
            self.chinese.set(info['chinese'])
            self.math.set(info['math'])
            self.english.set(info['english'])
            self.status.set('数据查询成功')
        else:
            self.status.set(info)

    def change_user(self):
        stu = {{"name": self.name.get(), "chinese": self.chinese.get(),
               "math": self.math.get(), "english": self.english.get()}}
        self.name.set('')  # 录入后刷新旧数据
        self.chinese.set('')
        self.math.set('')
        self.english.set('')
        ck.update(stu)  # 更新数据
        self.status.set('修改数据成功')


class InsertFrame(tk.Frame):    # 录入模块
    def __init__(self, root):
        super().__init__(root)
        self.name = tk.StringVar()
        self.chinese = tk.StringVar()
        self.math = tk.StringVar()
        self.english = tk.StringVar()
        self.status = tk.StringVar()

    def create_page(self):
        tk.Label(self).grid(row=1, pady=10)

        tk.Label(self, text='姓 名').grid(row=1, column=1, pady=10)
        tk.Entry(self, textvariable=self.name).grid(row=1, column=2, pady=10)
        tk.Label(self, text='语 文').grid(row=2, column=1, pady=10)
        tk.Entry(self, textvariable=self.chinese).grid(row=2, column=2, pady=10)
        tk.Label(self, text='数 学').grid(row=3, column=1, pady=10)
        tk.Entry(self, textvariable=self.math).grid(row=3, column=2, pady=10)
        tk.Label(self, text='英 语').grid(row=4, column=1, pady=10)
        tk.Entry(self, textvariable=self.english).grid(row=4, column=2, pady=10)

        tk.Button(self, text='录入', command=self.recode_info).grid(row=5, column=2, pady=10)
        tk.Label(self, textvariable=self.status).grid(row=6, column=2, pady=10, sticky=tk.E)

    def recode_info(self):  # 信息录入
        stu = {"name": self.name.get(), "chinese": self.chinese.get(),
               "math": self.math.get(), "english": self.english.get()}
        self.name.set('')  # 录入后刷新旧数据
        self.chinese.set('')
        self.math.set('')
        self.english.set('')
        ck.insert(stu)
        self.status.set('录入数据成功')


class SearchFrame(tk.Frame):    # 查询模块
    def __init__(self, root):
        super().__init__(root)  # 插入
        self.tree_view = None
        self.table_view = tk.Frame
        self.table_view.pack()
        self.create_page()

    def create_page(self):
        columns = ("name", "chinese", "math", "english")
        columns_values = ("姓名", "语文", "数学", "英语")
        self.tree_view = ttk.Treeview(self, show='headings', columns=columns)
        self.tree_view.column('name', width=80, anchor='center')
        self.tree_view.column('chinese', width=80, anchor='center')
        self.tree_view.column('math', width=80, anchor='center')
        self.tree_view.column('english', width=80, anchor='center')
        self.tree_view.column('name', width=80, anchor='center')
        self.tree_view.heading('name', text='姓名')
        self.tree_view.heading('chinese', text='语文')
        self.tree_view.heading('math', text='数学')
        self.tree_view.heading('english', text='英语')
        self.tree_view.pack(fill=tk.BOTH, expand=True)
        self.show_date_frame()

        tk.Button(self, text='刷新数据', command=self.show_date_frame).pack(anchor=tk.E, pady=5)

    def show_date_frame(self):
        # 删除旧节点
        for _ in map(self.tree_view.delete, self.tree_view.get_children('')):
            pass

        students = ck.all()
        index = 0
        for stu in students:
            self.tree_view.insert('', index + 1, values=(
                stu['name'], stu['chinese'], stu['math'], stu['english']
            ))

        tk.Label(self, text='查询页面').pack()


class DeleteFrame(tk.Frame):  # 删除模块
    def __init__(self, root):
        super().__init__(root)
        self.username = tk.StringVar()
        self.status = tk.StringVar()
        tk.Label(self, text='请根据名字删除数据').pack()
        tk.Entry(self, textvariable=self.username).pack()
        tk.Button(self, text='删除', command=self.delete).pack()
        tk.Label(self, textvariable=self.status).pack()

    def delete(self):
        username = self.username.get()
        flag, message = ck.delete_by_username(username)
        self.status.set(message)

还有两个数据库模块
第一个请命名为uses.json
第二个命名为students.json

[{"username": "admin", "password": "123456"}, {"username": "xiaoming", "password": "123456"}]

[{"name": "张三", "chinese": "47","math": "60","english": "47"}]
```python 
# 此模块报错
import tkinter as tk
from views import AboutFrame, ChangeFrame, InsertFrame, SearchFrame, DeleteFrame


# 以下是主页面程序模块

class MianPage:
    def __init__(self, master: tk.Tk):
        self.delete_frame = None
        self.insert_frame = None
        self.about_frame = None
        self.search_frame = None
        self.change_frame = None
        self.root = master
        self.root.title('学生信息管理系统(SIMI)')
        self.root.geometry('600x400')
        self.create_page()

    def create_page(self):  # 在views中封装
        self.about_frame = AboutFrame(self.root)  # 关于
        self.change_frame = ChangeFrame(self.root)
        self.insert_frame = InsertFrame(self.root)
        self.search_frame = SearchFrame(self.root)
        self.delete_frame = DeleteFrame(self.root)

        # 菜单栏
        menubar = tk.Menu(self.root)
        menubar.add_command(label='录入', command=self.show_insert)
        menubar.add_command(label='查询', command=self.show_search)
        menubar.add_command(label='删除', command=self.show_delete)
        menubar.add_command(label='修改', command=self.show_change)
        menubar.add_command(label='关于', command=self.show_about)
        self.root['menu'] = menubar

    # 页面显示,切换逻辑
    def show_insert(self):
        self.insert_frame.pack()    # pack_forget()目的是显示此页面后隐藏上一页面
        self.change_frame.pack_forget()
        self.search_frame.pack_forget()
        self.delete_frame.pack_forget()
        self.about_frame.pack_forget()

    def show_search(self):
        self.insert_frame.pack_forget()
        self.change_frame.pack_forget()
        self.search_frame.pack()
        self.delete_frame.pack_forget()
        self.about_frame.pack_forget()

    def show_delete(self):
        self.insert_frame.pack_forget()
        self.change_frame.pack_forget()
        self.search_frame.pack_forget()
        self.delete_frame.pack()
        self.about_frame.pack_forget()

    def show_change(self):
        self.insert_frame.pack_forget()
        self.change_frame.pack()
        self.search_frame.pack_forget()
        self.delete_frame.pack_forget()
        self.about_frame.pack_forget()

    def show_about(self):
        self.insert_frame.pack_forget()
        self.change_frame.pack_forget()
        self.search_frame.pack_forget()
        self.delete_frame.pack_forget()
        self.about_frame.pack()


if __name__ == '__main__':
    root = tk.Tk()
    MianPage(root)
    root.mainloop()


img

找不到哪个类出问题啊

  • 写回答

2条回答 默认 最新

  • Smart-Space 2022-12-24 10:14
    关注

    views.py的101行self.table_view = tk.Frame应该是self.table_view = tk.Frame(...)将类实例,当然我也就没看明白你对这个控件想要安放在哪个父组件之下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月1日
  • 已采纳回答 12月24日
  • 创建了问题 12月24日

悬赏问题

  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。