我创建了全局变量名称计数
import tkinter as tk
from tkinter import ttk
import os
from csv import DictWriter, DictReader
data_label_frame=tk.LabelFrame()
data_label_frame.pack()
LARGE_FONT = ('Verdana' , 16,'bold')
global count
count=0
class sea_of_btc(tk.Tk):
def __init__(self , *args , **kwargs):
tk.Tk.__init__(self, *args , **kwargs)
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.config(background='#FAEBEF')
self.frames = {}
for F in (start_page , page_one ):
frame = F(container , self)
self.frames [F] = frame
frame.grid (row = 0 , column = 0 , sticky= "nsew")
self.show_frame(start_page)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
return frame
class start_page(tk.Frame):
def __init__(self , parent , controller ):
tk.Frame.__init__(self, parent)
现在,我希望每当调用register_func时,计数都会增加,此后,每当我打开框架时,它就会在输入框中显示该数字,并且用户无法更改该数字,这意味着该数字仅适用于串行输入框中显示的数字
class page_one (tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
registration_number = tk.Label(data_label_frame, text = 'Registration No :', font = LARGE_FONT)
registration_number.grid(row=0,column=0,padx=5 , pady=5 , sticky =tk.W )
registration_number_var=tk.StringVar()
registration_number_entry = ttk.Entry(data_label_frame,width = 5, font=LARGE_FONT ,textvariable=registration_number_var,
)
registration_number_entry.grid(row =1, column =0,padx=10,pady=10 , ipady=5 , sticky=tk.E)
def register_fucn():
count += 1
with open('new.csv', 'a', newline='') as data_file:
dict_writer = DictWriter(data_file, fieldnames=['Registration No',
])
registar_btn = ttk.Button(data_label_frame , text ='Registar' ,command= register_fucn)
registar_btn.grid(row=2 , column=1, sticky =tk.E , padx=10 , pady=10,ipady=5)
由于@HardikJain解决了您的第一个问题,我可以为您解决第二个问题。使用户无法更改号码所需要做的一切,只需在函数中将state
小部件的Entry
更改为DISABLED
,然后更改其中的文本即可。