python - Global variables and appending for use in other top level functions -


i'm trying create "admin" section small program executes maths.

the admin button on main tk window creates top level window entry field turns on when correct password entered password field (or @ least when figure out how this.)

the submit button intended update global variable of price remembered program entry field have new price input user. issue i'm having how make global variable update , change , stay changed once button pressed.

this code designed test ability sake of context post here anyways. towards goal fantastic.

the issue code not work, wont allow me alter global variables, , produces error variable int has no attribute append?

further - append wrong move, fair enough, problem have global12mmprice = 200 not updating globalvariable , @ other points in program still referencing original value. there way update global variable program reflect new value , old 1 no longer exist?

global12mmprice = 86.67 global15mmprice = 191.19 int12mmprice = int(global12mmprice) int15mmprice = int(global15mmprice)   class mainwindow(tk.frame):     def __init__(self, root):         tk.frame.__init__(self, root)         b1 = tk.button(self, text="glass table", command = self.glsqwindow)         b1.grid(column=1,row=2,pady=50,padx=10)         self.count = 0         b2 = tk.button(self, text='round table', command = self.glrnwindow)         b2.grid(column=2,row=2,pady=50,padx=10)         self.count = 0         b3 = tk.button(self, text='console table', command = self.glcnwindow)         b3.grid(column=3,row=2,pady=50,padx=10)         self.count = 0         b4 = tk.button(self, text='admin', command = self.admin)         b4.grid(column=4,row=2,pady=50,padx=10)         self.count = 0       def admin(self):                self.count += 1         window = tk.toplevel(self)         window.geometry("600x350+300+300")          def submit():             int12mmprice.append(200)           b1 = tk.button(window,text='submit', command=submit)         b1.grid(column=3,row=2,pady=50,padx=10) 

there alot more code after relevant part. general advice might have of course welcome.

answer:- provided alot of assistance "fdhsdrg". solution implemented desired result has question in future.

as explained me needed create file program read , write create necessary information program access , alter , when needed.

import tkinter tk tkinter import * tkinter import tk, frame, menu import tkinter.messagebox box import pickle, os  file=open('prices.dat','rb') data=pickle.load(file) file.close global12mmprice = data[0] global15mmprice = data[1]  class mainwindow(tk.frame):     def __init__(self, root):         tk.frame.__init__(self, root)         b1 = tk.button(self, text="glass table", command = self.glsqwindow)         b1.grid(column=1,row=2,pady=50,padx=10)         self.count = 0         b2 = tk.button(self, text='round table', command = self.glrnwindow)         b2.grid(column=2,row=2,pady=50,padx=10)         self.count = 0         b3 = tk.button(self, text='console table', command = self.glcnwindow)         b3.grid(column=3,row=2,pady=50,padx=10)         self.count = 0         b4 = tk.button(self, text='admin', command = self.admin)         b4.grid(column=4,row=2,pady=50,padx=10)         self.count = 0       def admin(self):                self.count += 1         window = tk.toplevel(self)         window.geometry("600x350+300+300")            def submit():             global data             data[0] = '86.67'             file=open('prices.dat','wb')             pickle.dump(data,file)             file.close                     global root             box.showinfo('administration','the program terminate , prices                   updated.')             root.destroy()         b1 = tk.button(window,text='submit', command=submit)         b1.grid(column=3,row=2,pady=50,padx=10) 

as can see data list in .dat file gets updated, later replace get.entry() field demonstrates intended design. might want consider using resetboard instead of destroy if want program automatically relaunch after closing.

well, error message added pretty explains everything. int12mmprice integer, not have method append. append method can used on objects of type list:

>>> a=9 >>> type(a) <type 'int'> >>> a.append(15) traceback (most recent call last):   file "<pyshell#1>", line 1, in <module>     a.append(15) attributeerror: 'int' object has no attribute 'append'  >>> a=[9] >>> type(a) <type 'list'> >>> a.append(15) >>> [9, 15] 

edit:

right, problem of scopes. edit global int12mmprice put global int12mmprice @ start of submit function. makes sure submit not @ int12mmprice in own function scope in global scope.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -