在python中共享变量

时间:2018-06-26 12:12:51

标签: python tkinter

我正在尝试将变量tkinter与全局代码共享, 我有一个tkinter gui,我在其中插入数据并关闭class App: def __init__(self, master): self.username = None self.frame1 = tk.LabelFrame (master, text="Instagram") self.frame1.pack () self.dict = {'':[], 'Scienza': ['Nasa', '2', '3'], 'Moda': ['vladislavkirillov', 'barengo1', 'vittorio__badia'], 'Dunk Page': ['stupidpikachu', 'nascecresceignora', 'the_fucking_dank']} self.categoria_pagina = tk.StringVar() self.pagina_preimpostata = tk.StringVar() self.username1 = tk.StringVar() self.password = tk.StringVar() self.pagina_tua = tk.StringVar() self.commento1 = tk.StringVar() Entry (self.frame1, textvariable=self.username1, bg="white").grid (column=1, row=2, sticky=tk.W) Entry (self.frame1, textvariable=self.password, bg="white").grid (column=1, row=3, sticky=tk.W) Entry (self.frame1, textvariable=self.pagina_tua, bg="white").grid (column=1, row=4, sticky=tk.W) self.categoria_pagina.trace('w', self.updateoptions) self.optionmenu_a = ttk.OptionMenu(self.frame1, self.categoria_pagina, *self.dict.keys()) self.categoria_pagina.set("Scienza") self.optionmenu_a.grid(row = 7, column = 1) self.optionmenu_b = ttk.OptionMenu(self.frame1, self.pagina_preimpostata, '') self.optionmenu_b.grid(row = 8, column = 1) self.variable_c = tk.StringVar () self.variable_d = tk.StringVar () def updateoptions(self, *args): countries = self.dict[self.categoria_pagina.get()] self.pagina_preimpostata.set(countries[0]) menu = self.optionmenu_b['menu'] menu.delete(0, 'end') for country in countries: menu.add_command(label=country, command=lambda country=country: self.pagina_preimpostata.set(country)) categoria = self.categoria_pagina.get() pagina_preimpostata_stringa = self.pagina_preimpostata.get() username= self.username1.get() password= self.password.get() pagina_tua=self.pagina_tua.get() root = tk.Tk() app = App(root) root.mainloop() print(app.username) 表单,我希望代码打印此数据,但是该代码不接受我输入的数据,并返回另一个值

这是代码:

tkinter

它返回值None代替我在document.getElementById("email").value = 'change@change.com' ; document.getElementById("password").value = 'change1234' ; gui中插入的值

1 个答案:

答案 0 :(得分:0)

问题在于,当您在代码中执行以下操作

git ls-remote

Python在函数范围内创建一个局部变量,当函数结束时,该变量不再存在。因此,您有多个解决方案。

一种方法是在username= self.username1.get() 的{​​{1}}中定义username

__init__

在您的函数中使用

App

随后您可以使用self.username = None

另一种选择是使用全局变量(不推荐)

self.username = self.username1.get()