以下是修改后的代码,其中主要是将mainwindow()函数中的代码移动到了函数外部,避免了函数嵌套导致的问题:

import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
import pandas as pd 
import numpy as np
from tkinter import *
from tkinter import filedialog
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
import matplotlib.pyplot as plt
from pyecharts.charts import Pie
from pyecharts.charts import Bar
from sklearn.ensemble import RandomForestClassifier
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk


root = tk.Tk()
root.geometry('500x400+500+300')
root.title('登录页')

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

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

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

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


def login():
    name = username.get()
    pwd = password.get()
    print(name, pwd)
        
    
    if name == 'admin' and pwd == '123456':
            
        messagebox.showwarning(title='验证', message='登陆成功')
        print('登陆成功')
        root.destroy()
        mainwindow()
    else:
        messagebox.showwarning(title='警告', message='登陆失败')


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

def mainwindow():
    mainwindow.root = tk.Tk()
    mainwindow.root.geometry('500x250')
    mainwindow.root.title('数据处理系统')

    page2 = tk.Frame(mainwindow.root)

    width = 600
    height = 400
    screenwidth = mainwindow.root.winfo_screenwidth()
    screenheight = mainwindow.root.winfo_screenheight()
    x = (screenwidth - width) / 2
    y = (screenheight - height) / 2
    mainwindow.root.geometry("%dx%d+%d+%d" % (width, height, x, y))

    # 添加欢迎文本
    name_label = tk.Label(mainwindow.root, text="欢迎进入系统", bg="pink", fg="white", font=("Arial", 24))
    name_label.pack(pady=50)


    # create a menu bar
    menu_bar = tk.Menu(mainwindow.root)
    mainwindow.root.config(menu=menu_bar)

    # create a file menu
    file_menu = tk.Menu(menu_bar)
    
    def shujuyuchuli():
        yuchuliwindow()

    def huitu():
        huituwindow()

    menu_bar.add_cascade(label="菜单:", menu=file_menu)
    menu_bar.add_command(label="数据预处理", command=shujuyuchuli)
    menu_bar.add_command(label="绘制图像",command=huitu)

    mainwindow.root.mainloop()

def yuchuliwindow():
   # GUI界面
    root = tk.Tk()
    root.title("数据预处理和模型训练")
    root.geometry("400x400")

    # 文件选择函数
    def select_file():
        filepath = filedialog.askopenfilename()
        file_entry.delete(0, tk.END)
        file_entry.insert(0, filepath)

    # 数据读取和预处理函数
    def process_data():
        # 读入数据集
        filepath = file_entry.get()
        df = pd.rea
import tkinter as tkfrom tkinter import messageboxfrom PIL import Image ImageTkimport pandas as pd import numpy as npfrom tkinter import from tkinter import filedialogfrom sklearn import preprocessing

原文地址: https://www.cveoy.top/t/topic/fFg7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录