gpt4 book ai didi

python - 如何禁用和启用 python tkinter 框架中的所有小部件

转载 作者:行者123 更新时间:2023-12-04 08:51:24 28 4
gpt4 key购买 nike

from tkinter import *
from tkinter import ttk
import mysql.connector
from tkinter import messagebox

mydb = mysql.connector.connect(host="localhost", user="root", passwd="")
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE IF NOT EXISTS STUDENT_RECORDS")
mycursor.execute("USE STUDENT_RECORDS")
mycursor.execute("CREATE TABLE IF NOT EXISTS STU (ROLL_NO INT, NAME VARCHAR(255), EMAIL VARCHAR(255), GENDER VARCHAR(255), CONTACT VARCHAR(255), DOB VARCHAR(255), ADDRESS VARCHAR(255))")

class Students:
def __init__(self, root):
self.root = root
self.root.title("Student Management System")
self.root.geometry("1350x700+0+0")
color = "blue"

title = Label(self.root, text="Student Management System", bd=10, relief=GROOVE, font=("times new roman", 40, "bold"), bg="yellow", fg="red")
title.pack(side=TOP, fill=X)

#=================All Variables
self.Roll_no_var = StringVar()
self.name_var = StringVar()
self.email_var = StringVar()
self.gender_var = StringVar()
self.contact_var = StringVar()
self.dob_var = StringVar()

self.searchby_var = StringVar()
self.searchtxt_var = StringVar()


#=================MANAGE FRAME
self.Manage_Frame = Frame(self.root, bd=4, relief=RIDGE, bg=color)
self.Manage_Frame.place(x=20,y=100,width=450,height=600)

m_title = Label(self.Manage_Frame, text="Manage Students", bd=10, font=("times new roman", 30, "bold"), bg=color, fg="white")
m_title.grid(row=0,columnspan=2,pady=20)

lbl_roll = Label(self.Manage_Frame, text="ROLL NO.", font=("times new roman", 20, "bold"), bg=color, fg="white")
lbl_roll.grid(row=1, column=0, pady=10, padx=20, sticky="w")

txt_roll = Entry(self.Manage_Frame, textvariable=self.Roll_no_var, font=("times new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_roll.grid(row=1, column=1, pady=10, padx=20, sticky="w")

lbl_name = Label(self.Manage_Frame, text="Name", font=("times new roman", 20, "bold"), bg=color, fg="white")
lbl_name.grid(row=2, column=0, pady=10, padx=20, sticky="w")
print(type(lbl_name))
txt_name = Entry(self.Manage_Frame, textvariable=self.name_var, font=("times new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_name.grid(row=2, column=1, pady=10, padx=20, sticky="w")

lbl_Email = Label(self.Manage_Frame, text="Email", font=("times new roman", 20, "bold"), bg=color, fg="white")
lbl_Email.grid(row=3, column=0, pady=10, padx=20, sticky="w")

txt_Email = Entry(self.Manage_Frame, textvariable=self.email_var, font=("times new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_Email.grid(row=3, column=1, pady=10, padx=20, sticky="w")

lbl_gender = Label(self.Manage_Frame, text="Gender", font=("times new roman", 20, "bold"), bg=color, fg="white")
lbl_gender.grid(row=4, column=0, pady=10, padx=20, sticky="w")

combo_gender = ttk.Combobox(self.Manage_Frame, textvariable=self.gender_var, font=("times new roman", 13, "bold"), state="readonly")
combo_gender['values']=("Male", "Female", "Other")
combo_gender.grid(row=4, column=1, pady=10, padx=20)

lbl_contact = Label(self.Manage_Frame, text="Contact", font=("times new roman", 20, "bold"), bg=color, fg="white")
lbl_contact.grid(row=5, column=0, pady=10, padx=20, sticky="w")

txt_contact = Entry(self.Manage_Frame, textvariable=self.contact_var, font=("times new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_contact.grid(row=5, column=1, pady=10, padx=20, sticky="w")

lbl_dob = Label(self.Manage_Frame, text="D.O.B", font=("times new roman", 20, "bold"), bg=color, fg="white")
lbl_dob.grid(row=6, column=0, pady=10, padx=20, sticky="w")

txt_dob = Entry(self.Manage_Frame, textvariable=self.dob_var, font=("times new roman", 15, "bold"), bd=5, relief=GROOVE)
txt_dob.grid(row=6, column=1, pady=10, padx=20, sticky="w")

lbl_address = Label(self.Manage_Frame, text="Address", font=("times new roman", 20, "bold"), bg=color, fg="white")
lbl_address.grid(row=7, column=0, pady=10, padx=20, sticky="w")

self.txt_address = Text(self.Manage_Frame, font=("", 10, "bold"), height=4, width=30)
self.txt_address.grid(row=7, column=1, pady=10, padx=20, sticky="w")

#=================BUTTON FRAME
btn_Frame = Frame(self.Manage_Frame, bd=4, relief=RIDGE, bg=color)
btn_Frame.place(x=18,y=540,width=420)

add_btn = Button(btn_Frame, text="ADD", width=10).grid(row=0, column=0, padx=10, pady=5)
update_btn = Button(btn_Frame, text="UPDATE", width=10).grid(row=0, column=1, padx=10, pady=5)
delete_btn = Button(btn_Frame, text="DELETE", width=10).grid(row=0, column=2, padx=10, pady=5)
clear_btn = Button(btn_Frame, text="CLEAR", width=10).grid(row=0, column=3, padx=10, pady=5)

#==================ON OFF BUTTON FRAME
on_off_btn_Frame = Frame(self.root, bd=4, relief=RIDGE, bg='red')
on_off_btn_Frame.place(x=1305,y=100,width=55, height=70)

on_btn = Button(on_off_btn_Frame, text='ON', command=self.on_all).pack(fill=X, side=TOP)
off_btn = Button(on_off_btn_Frame, text='OFF', command=self.off_all).pack(fill=X, side=BOTTOM)

#=================TABLE FRAME
Table_Frame = Frame(self.root, bd=4, relief=RIDGE, bg="crimson")
Table_Frame.place(x=500,y=110, width=760, height=500)

scrollx = Scrollbar(Table_Frame, orient=HORIZONTAL)
scrolly = Scrollbar(Table_Frame, orient=VERTICAL)
self.Student_table = ttk.Treeview(Table_Frame, columns=("Roll", "Name", "Email", "Gender", "Contact", "dob", "Address"), xscrollcommand=scrollx.set, yscrollcommand=scrolly.set)
scrollx.pack(side=BOTTOM, fill=X)
scrolly.pack(side=RIGHT, fill=Y)
scrollx.config(command=self.Student_table.xview)
scrolly.config(command=self.Student_table.yview)
self.Student_table.heading("Roll", text="Roll No.")
self.Student_table.heading("Name", text="Name")
self.Student_table.heading("Email", text="Email")
self.Student_table.heading("Gender", text="Gender")
self.Student_table.heading("Contact", text="Contact")
self.Student_table.heading("dob", text="D.O.B")
self.Student_table.heading("Address", text="Address")

self.Student_table['show']='headings'
self.Student_table.column("Roll", width=100)
self.Student_table.column("Name", width=100)
self.Student_table.column("Email", width=100)
self.Student_table.column("Gender", width=100)
self.Student_table.column("Contact", width=100)
self.Student_table.column("dob", width=100)
self.Student_table.column("Address", width=150)
self.Student_table.pack(fill=BOTH, expand=1)

def on_all(self):
for child in self.Manage_Frame.winfo_children():
child.configure(state='normal')

def off_all(self):
for child in self.Manage_Frame.winfo_children():
child.configure(state='disabled')



root = Tk()
st = Students(root)
root.mainloop()

在这部分我收到错误。
    def on_all(self):
for child in self.Detail_Frame.winfo_children():
child.configure(state='normal')

def off_all(self):
for child in self.Detail_Frame.winfo_children():
child.configure(state='disabled')
错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Manish Gusain\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/Manish Gusain/PycharmProjects/Student_Management_System.py", line 233, in off_all
child.configure(state='disabled')
File "C:\Users\Manish Gusain\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1485, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\Manish Gusain\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1476, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-state"
我想要做的是当我点击关闭按钮时,框架中的所有小部件都应该被禁用,当我点击打开按钮时,框架中的所有小部件都应该再次启用。但是当我单击“关闭”按钮或“打开”按钮时,它会显示此错误。
其次,如何在单击隐藏按钮时隐藏 tkinter 主窗口中除标题之外的所有内容,并在单击显示按钮时再次显示所有内容。

最佳答案

关于你的两个问题:

  • Frame 对象没有 state属性,以便您可以检查小部件是否为框架并跳过更新。您还可以使用 try\except 块来捕获错误。下面的代码两者都做。
  • 要隐藏主框架,只需将其移出屏幕,使其在窗体上不可见

  • 这是更新的代码。您需要添加 selfTable_Frame引用,因此可以在 on\off 方法中访问它。
        self.Table_Frame = Frame(self.root, bd=4, relief=RIDGE, bg="crimson")  # chnge to self.Table_Frame
    self.Table_Frame.place(x=500,y=110, width=760, height=500)

    scrollx = Scrollbar(self.Table_Frame, orient=HORIZONTAL)
    scrolly = Scrollbar(self.Table_Frame, orient=VERTICAL)
    self.Student_table = ttk.Treeview(self.Table_Frame, columns=("Roll", "Name", "Email", "Gender", "Contact", "dob", "Address"), xscrollcommand=scrollx.set, yscrollcommand=scrolly.set)

    .............

    def on_all(self):
    self.Table_Frame.place(x=500,y=110, width=760, height=500) # on screen
    for child in self.Manage_Frame.winfo_children():
    try:
    if child.widgetName != 'frame': # frame has no state, so skip
    child.configure(state='normal')
    except Exception as e:
    print(e)

    def off_all(self):
    self.Table_Frame.place(x=5000,y=110, width=760, height=500) # off screen
    for child in self.Manage_Frame.winfo_children():
    try:
    if child.widgetName != 'frame': # frame has no state, so skip
    child.configure(state='disabled')
    except Exception as e:
    print(e)
    输出(开)
    On
    输出(关闭)
    Off

    关于python - 如何禁用和启用 python tkinter 框架中的所有小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64080833/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com