猿问

为什么不显示 tkinter 窗口?

我尝试构建一个学生管理系统,而此 tkinter 窗口在运行以下代码后未显示。有人可以在这里提出一些建议吗?这是来自 youtube chanel 的公共源代码。



from tkinter import *

from tkinter import ttk


class Student:

    def __init__(self,root):

        self.root=root

        self.root.title("student management system")

        self.root.geometry("1350*700+0+0")


        title=Label(self.root,text="Student Management System",bd=10,relief=GROOVE,front=("time new roman",40,"bold"),bg="yellow",fg="red")

        title.pack(side=TOP,fill=X)


        Manage_Frame=Frame(self.root,bd=4,relief=RIDGE,bg="crimson")

        Manage_Frame.place(x=20,y=70,width=450,height=560)


    #========Massage Frame===============================

       Manage_Frame=Frame(self.root,bd=4,relief=RIDGE,bg="crimson")

        Manage_Frame.place(x=20,y=100,width=450,height=560)


        m_title=Label(Manage_Frame,text="Manage Students",bg="crimson",fg="white",front=("time new roman",30,"bold"))

        m_title.grid(row=0,columnspan=2,pady=10)


        lbl_roll = Label(Manage_Frame, text="Roll No.", bg="crimson", fg="white",front=("time new roman", 20, "bold"))

        lbl_roll.grid(row=1, colum=0, pady=10,padx=20,sticky="w")


        txt_roll = Entry(Manage_Frame,front=("time new roman", 15, "bold"),bd=5,relief=GROOVE)

        txt_roll.grid(row=1, colum=2, pady=10, padx=20, sticky="w")


        lbl_name = Label(Manage_Frame, text="Name", bg="crimson", fg="white", front=("time new roman", 20, "bold"))

        lbl_name.grid(row=1, colum=0, pady=10, padx=20, sticky="w")


        txt_name = Entry(Manage_Frame, front=("time new roman", 15, "bold"), bd=5, relief=GROOVE)

        txt_name.grid(row=2, colum=1, pady=10, padx=20, sticky="w")


        lbl_Email = Label(Manage_Frame, text="Email", bg="crimson", fg="white", front=("time new roman", 20, "bold"))

        lbl_Email.grid(row=1, colum=0, pady=10, padx=20, sticky="w")


        txt_Email = Entry(Manage_Frame, front=("time new roman", 15, "bold"), bd=5, relief=GROOVE)

        txt_Email.grid(row=3, colum=0, pady=10, padx=20, sticky="w")

沧海一幻觉
浏览 56回答 1
1回答

一只名叫tom的猫

您必须初始化 Student 对象并将 root 传递给它。将此添加到代码的末尾以使其运行:app=Student(root)root.mainloop()您当前的代码包含许多语法错误,需要在运行前修复。以下应该运行,尽管有一些视觉错误:from tkinter import *from tkinter import ttkclass Student:    def __init__(self,root):        self.root=root        self.root.title("student management system")        self.root.geometry("1350x700")        title=Label(self.root,text="Student Management System",bd=10,relief=GROOVE,font=("time new roman",40,"bold"),bg="yellow",fg="red")        title.pack(side=TOP,fill=X)        Manage_Frame=Frame(self.root,bd=4,relief=RIDGE,bg="crimson")        Manage_Frame.place(x=20,y=70,width=450,height=560)    #========Massage Frame===============================        Manage_Frame=Frame(self.root,bd=4,relief=RIDGE,bg="crimson")        Manage_Frame.place(x=20,y=100,width=450,height=560)        m_title=Label(Manage_Frame,text="Manage Students",bg="crimson",fg="white",font=("time new roman",30,"bold"))        m_title.grid(row=0,columnspan=2,pady=10)        lbl_roll = Label(Manage_Frame, text="Roll No.", bg="crimson", fg="white",font=("time new roman", 20, "bold"))        lbl_roll.grid(row=1, column=0, pady=10,padx=20,sticky="w")        txt_roll = Entry(Manage_Frame,font=("time new roman", 15, "bold"),bd=5,relief=GROOVE)        txt_roll.grid(row=1, column=2, pady=10, padx=20, sticky="w")        lbl_name = Label(Manage_Frame, text="Name", bg="crimson", fg="white", font=("time new roman", 20, "bold"))        lbl_name.grid(row=1, column=0, pady=10, padx=20, sticky="w")        txt_name = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)        txt_name.grid(row=2, column=1, pady=10, padx=20, sticky="w")        lbl_Email = Label(Manage_Frame, text="Email", bg="crimson", fg="white", font=("time new roman", 20, "bold"))        lbl_Email.grid(row=1, column=0, pady=10, padx=20, sticky="w")        txt_Email = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)        txt_Email.grid(row=3, column=0, pady=10, padx=20, sticky="w")        lbl_Gender = Label(Manage_Frame, text="Gender", bg="crimson", fg="white", font=("time new roman", 20, "bold"))        lbl_Gender.grid(row=4, column=0, pady=10, padx=20, sticky="w")        combo_Gender=ttk.Combobox(Manage_Frame,font=("times new roman",20,"bold"))        combo_Gender['values']=("Male","Female","other")        combo_Gender.grid(row=4,column=1,padx=20,pady=10)        lbl_Contact = Label(Manage_Frame, text="Contact", bg="crimson", fg="white", font=("time new roman", 20, "bold"))        lbl_Contact.grid(row=5, column=0, pady=10, padx=20, sticky="w")        txt_Contact = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)        txt_Contact.grid(row=5, column=1, pady=10, padx=20, sticky="w")        lbl_DOB = Label(Manage_Frame, text="DOB", bg="crimson", fg="white",font=("time new roman", 20, "bold"))        lbl_DOB.grid(row=6, column=0, pady=10, padx=20, sticky="w")        txt_Contact = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)        txt_Contact.grid(row=6, column=1, pady=10, padx=20, sticky="w")        lbl_address = Label(Manage_Frame, text="Address", bg="crimson", fg="white", font=("time new roman", 20, "bold"))        lbl_address.grid(row=6, column=0, pady=10, padx=20, sticky="w")        txt_address = Entry(Manage_Frame, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)        txt_address.grid(row=6, column=1, pady=10, padx=20, sticky="w")    #=========Button Frame=========        btn_Frame=Frame(Manage_Frame,bd=4,relief=RIDGE,bg="crimson")        btn_Frame.place(x=15, y=500, width=420)        Addbtn = Button(btn_Frame,text="add",width=10).grid(row=0,column=0,padx=10,pady=10)        Updatebtn = Button(btn_Frame, text="update", width=10).grid(row=0, column=1, padx=10, pady=10)        Deletebtn = Button(btn_Frame, text="delete", width=10).grid(row=0, column=2, padx=10, pady=10)        Clearbtn = Button(btn_Frame, text="clear", width=10).grid(row=0, column=3, padx=10, pady=10)    #=========Detail Frame=========        Detail_Frame = Frame(self.root, bd=4, relief=RIDGE, bg="crimson")        Detail_Frame.place(x=500, y=100, width=800, height=580)        lbl_search = Label(Detail_Frame, text="Search By", bg="crimson", fg="white", font=("time new roman", 20, "bold"))        lbl_search.grid(row=0, column=0, pady=10, padx=20, sticky="w")        combo_search = ttk.Combobox(Manage_Frame,width=10,font=("times new roman", 13, "bold"),state="readonly")        combo_search['values'] = ("Roll", "Name", "contact")        combo_search.grid(row=0, column=1, padx=20, pady=10)        txt_Search = Entry(Manage_Frame,width=15, font=("time new roman", 15, "bold"), bd=5, relief=GROOVE)        txt_Search.grid(row=6, column=1, pady=10, padx=20, sticky="w")        searchbtn = Button(btn_Frame, text="Search", width=10).grid(row=0, column=3, padx=10, pady=10)        showallbtn = Button(btn_Frame, text="Show All", width=10).grid(row=0, column=4, padx=10, pady=10)    #=========Table Frame=========        Table_Frame = Frame(Detail_Frame, bd=4, relief=RIDGE, bg="crimson")        Table_Frame.place(x=10, y=70, width=760, height=500)        scroll_x=Scrollbar(Table_Frame,orient=HORIZONTAL)        scroll_y = Scrollbar(Table_Frame, orient=VERTICAL)        Student_table=ttk.Treeview(Table_Frame,columns=("roll","name","email","gender","contact","dob","Address"),xscrollcommand=scroll_x.set,yscrollcommand=scroll_y.set)        scroll_x.pack(side=BOTTOM,fill=X)        scroll_y.pack(side=RIGHT, fill=Y)        scroll_x.config(command=Student_table.xview)        scroll_y.config(command=Student_table.xview)        Student_table.heading("roll",text="Roll")        Student_table.heading("name", text="Name")        Student_table.heading("email", text="Email")        Student_table.heading("gender", text="Gender")        Student_table.heading("contact", text="Contact")        Student_table.heading("dob", text="D.O.B")        Student_table.heading("Address", text="Address")        Student_table['show']='headings'        Student_table.pack()root=Tk()app=Student(root)root.mainloop()
随时随地看视频慕课网APP

相关分类

Python
我要回答