Python 全局变量代码混乱
我在从一个类到另一个类的数据输入中遇到了问题,而且我似乎无法使全局函数工作。我需要帮助的部分在最后一个函数中,但它只有在拥有完整代码的情况下才起作用Python 全局变量代码混乱,python,python-3.x,global-variables,Python,Python 3.x,Global Variables,我在从一个类到另一个类的数据输入中遇到了问题,而且我似乎无法使全局函数工作。我需要帮助的部分在最后一个函数中,但它只有在拥有完整代码的情况下才起作用 import tkinter as tk import os self = tk TITLE_FONT = ("AmericanTypewriter", 18, "bold") #exit function def Exit(): os._exit(0) #Functions end class SampleApp(tk.Tk
import tkinter as tk
import os
self = tk
TITLE_FONT = ("AmericanTypewriter", 18, "bold")
#exit function
def Exit():
os._exit(0)
#Functions end
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
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.frames = {}
for F in (Home, Population, Survival, Birth,NEW, data, Quit):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("Home")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class Home(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Home", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Population",
command=lambda: controller.show_frame("Population"))
button5 = tk.Button(self, text = "Quit",
command=lambda: controller.show_frame("Quit"))
button1.pack()
button5.pack()
class Population(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Enter Generation 0 Values", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
#Function
def EnterPopulation():
b1 = populationa.get()
print (str(populationa.get()))
bj = populationj.get()
print (str(populationj.get()))
bs = populations.get()
print (str(populations.get()))
def cal(*args):
total = population_juveniles + population_adults + population_seniles
#Population
population = tk.Label(self, text="Value for the Populations")
population.pack()
labela= tk.Label(self, text= 'Population of Adults')
populationa = tk.Entry(self)
labela.pack()
populationa.pack()
population3 = tk.Button(self, text="Enter", command = EnterPopulation)
population3.pack()
labelj= tk.Label(self, text= 'Population of Juvenile')
populationj = tk.Entry(self)
labelj.pack()
populationj.pack()
population5 = tk.Button(self, text="Enter", command = EnterPopulation)
population5.pack()
labels= tk.Label(self, text= 'Population of Seniles')
populations = tk.Entry(self)
labels.pack()
populations.pack()
population6 = tk.Button(self, text="Enter", command = EnterPopulation)
population6.pack()
buttonS = tk.Button(self, text = "Survival Rates",
command=lambda: controller.show_frame("Survival"))
buttonS.pack()
class Survival(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Survival Rates", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
def EnterSurvival(*self):
S = survivalaa.get()
ss = survivalj.get()
sss =survivals.get()
print(str(survivalaa.get()))
#Survival
Survival = tk.Label(self, text="Value of Survival Rates between 0-1")
Survival.pack()
survivala= tk.Label(self, text= 'Survival rates of Adults')
survivalaa = tk.Entry(self)
survivala.pack()
survivalaa.pack()
survival69= tk.Button(self, text="Enter", command = EnterSurvival)
survival69.pack()
survivaljj= tk.Label(self, text= 'Survival rates of Juvenile')
survivalj = tk.Entry(self)
survivaljj.pack()
survivalj.pack()
survival5 = tk.Button(self, text="Enter", command = EnterSurvival)
survival5.pack()
labelss= tk.Label(self, text= 'Survival rates of Seniles')
survivals = tk.Entry(self)
labelss.pack()
survivals.pack()
survival6 = tk.Button(self, text="Enter", command = EnterSurvival)
survival6.pack()
buttonS = tk.Button(self, text = "Birth Rates",
command=lambda: controller.show_frame("Birth"))
buttonS.pack()
class Birth(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Birth Rates", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
def EnterBirth(*args):
Birth1 = Birth2.get()
Birtha = tk.Label(self, text="Birth rates")
Birtha.pack()
Birth2 = tk.Entry(self)
Birth2.pack()
Birth3 = tk.Button(self, text="OK", command = EnterBirth)
Birth3.pack()
buttonB = tk.Button(self, text = "New Generatons",
command=lambda: controller.show_frame("NEW"))
buttonB.pack()
#Number of New Generations To Model
class NEW(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="New Generations", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
def EnterNew(*args):
print (New2.get())
news = New2.get()
New = tk.Label(self, text="Number of New Generatiions 5 - 25")
New.pack()
New2 = tk.Entry(self)
New2.pack()
New3 = tk.Button(self, text="OK", command = EnterNew)
New3.pack()
button = tk.Button(self, text="BACK To Home",
command=lambda: controller.show_frame("data"))
button.pack()
class data(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Data", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
no = tk.Button(self, text = "No",
command = lambda: controller.show_frame("Home"))
no.pack()
global Birth1
global s
global ss
global sss
global b1
global bj
global bs
global news
spja= b1 * s
spjb = bj * ss
spjs= bs * sss
st = spja + spjb + spjs
born = spja* rebirth
old = b1 + bj + bs
labelold = tk.Label(self, text = 'Orignal populaton'+str(old))
labelold.pack()
labelto = tk.Label(self, text='Adults that survived = '+str(spja)+ 'Juveniles = ' +str(spjb)+ 'Seniles = '+str(spjs)+ '/n Total= '+str(st))
labelto.pack()
Labelnew= tk.Label(self, text='New Juveniles = '+str(born))
Labelnew.pack()
# function for export data
class Quit(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Are you sure you want to quit?", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
yes = tk.Button(self, text="Yes", command = Exit)
yes.pack()
no = tk.Button(self, text = "No",
command = lambda: controller.show_frame("Home"))
no.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop(
如果要使用全局变量,需要执行以下操作:
var1 = <some value>
var2 = <some other value>
s1 = <yet another value here>
# then create your classes
class Whatever(tk.whatever_you_want):
def __init__(self, arg1, arg2, whatever_arg):
global var1 #whis tells the function to use the variable you defined already in the module scope
local_var_1 = var1 * 100 #
var1=
var2=
s1=
#然后创建您的类
分类任意(tk.任意你想要的):
定义初始化(self,arg1,arg2,任意参数):
全局变量1#whis告诉函数使用您在模块作用域中已经定义的变量
局部变量1=变量1*100
当然,也许你还不知道这一点,全局变量通常被认为是不好的开始,但我认为这对你来说并不重要。当你修改和读取变量时,这很重要,因为有太多地方需要跟踪,但我不确定你是否在构建这么大的脚本。这里的代码太多了。把它缩减到显示问题所需的最小值。你说的是什么全局函数?也。。。。你需要帮助解决什么问题?请更具体一点。请注意,类和函数有自己的作用域,因此您不能将在一个类中创建的变量当作全局变量传递给另一个类。您有一个方法,它使用了大量语句,如
global b1
和global s
。我认为您想要声明全局变量。。。。。但是,在python中它不是这样工作的。在python中不能声明变量。global语句做了一些完全不同的事情:它告诉函数不要创建一个局部变量,而是使用一个现有的全局变量。您希望第三行代码中奇怪的self=tk
做什么?因为OP使用的是类,所以几乎没有任何理由使用全局变量。他的问题表明对如何编码有更深的误解。是的。。。有些人是极端的初学者,OP非常难以解释。。。。所以我只是帮助他们回答问题,却忘了是我做的。我的意思是,也许这家伙只是想快速编写代码,而且永远不会对用Python方式编程的神秘智慧感兴趣。