我对 global 做的不多,我想知道如何使类中的函数和变量成为全局的。我试图使用字体和函数在类之间测试全局。如果有人能指出我哪里出错了,那将非常有帮助。
全局.py
#Fonts
#Common Functions
import tkFont
class Global():
def __init__(self):
global f,f1,f2,enter,leave
f = tkFont.Font(name='f',size=14, weight='bold')
f1 = tkFont.Font(name='f1',size=12, weight='bold')
f2 = tkFont.Font(name='f2', underline=True,size=12, weight='bold')
def enter(self,event):
event.widget.config(font='f2')
def leave(self,event):
event.widget.config(font='f1')
登录框架.py
from Tkinter import *
from Global import *
class LoginFrame(Frame):
def __init__(self,master):
self.master=master
Global()
def createWidgets(self):
self.frame = Frame(self.master,bg='black',width=800,height=500,bd=5,relief=GROOVE)
self.user_lbl = Label(self.frame, text='User', bg='black', fg='white',font='f1')
self.user_lbl.bind('<Enter>',enter), self.user_lbl.bind('<Leave>',leave)
self.pw_lbl = Label(self.frame, text='Password', bg='black', fg='white',font='f2')
self.pw_lbl.bind('<Enter>',enter), self.pw_lbl.bind('<Leave>',leave)
def packWidgets(self):
self.frame.grid_propagate(0), self.frame.grid(row=1)
self.user_lbl.grid(row=2,column=1,sticky=W)
self.pw_lbl.grid(row=4,column=1,sticky=W)
root=Tk()
loginFrame=LoginFrame(root)
loginFrame.createWidgets()
loginFrame.packWidgets()
root.mainloop()