Brandon Nadeau
Brandon Nadeau

Reputation: 3706

Python Global Variables Between Classes

I haven't done much with global and I was wondering how I can make the functions and variables inside a class global. I was trying to test global between classes using fonts and functions. If anyone can point out where i am going wrong, it would be very helpful.

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')

LoginFrame.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()

Upvotes: 0

Views: 3382

Answers (2)

dfb
dfb

Reputation: 13289

In this code

def __init__(self,master):
    self.master=master
    Global()

Global() is just created and not assigned. Global's __init__ calls

global f,f1,f2,enter,leave

But this defines the scope, not a 'global variable'.

One option is to do the following

class Globals():
    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')

or simply define them by themselves

 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')

and then use globals in your function. You don't need the keyword global unless you are making an assignment

self.pw_lbl = Label(...,f)

Upvotes: 1

Marcin
Marcin

Reputation: 49826

I haven't done much with global and I was wondering how I can make the functions and variables inside a class global.

You can't. Inside a method, you can declare a variable as global, and assignments to it will be to the global variable.

There isn't any other level of globalness.

Of course, you wouldn't want to do this, because the point of classes is to avoid global state, and to keep shared state encapsulated.

Upvotes: 1

Related Questions