Python Forum
Variable not sharing same value between two different functions Python 2.7 Tkinter
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable not sharing same value between two different functions Python 2.7 Tkinter
I would need to create a Python 2.7 script using Tkinter which should do the following:

When clicking on the "Create the NWs" button in a window -> I should see a field and a "Add network" button near the field. If I have more than one field to introduce, then I should press on the "Add network" button and see another field afterwards to fill in. If I have one or more than one field completed => should write in the file according to the "for" described in the define function. If I do not have any field completed, the file should not be created

The Tkinter part is doing what it should, but the code is not writing it correctly in the file. Here is the relevant part inside of a class where the problem is:

class Networks(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

    def create_widgets(self):
        label = tk.Label(self, text="Insert the name of the networks")
        start_button = tk.Button(self, text="Return to start page", command=lambda: master.switch_frame(StartPage))
        new_network_button = tk.Button(self, text="Add network", command=self.add_network)
        new_network_button.bind("<Return>", self.add_network)
        new_network_button.grid(row=len(self.master.networks), column=3, padx=4, pady=6, sticky="W")
        next_button=tk.Button(self, text="Next", command=self.networks)
        next_button.grid(row=1500, column=5,padx=4, pady=6, sticky="W")
        label.pack(side="top", fill="x", pady=10)
        for index, network in enumerate(self.master.networks):
            self.render_network_field(network, index)

    def add_network(self):
        self.master.networks.append({'variable': tk.StringVar(self.master)})

    def render_network_field(self, network, index):
        entry_field = tk.Entry(self, textvariable=network['variable'])
        entry_field.grid(row=index, column=0, columnspan=2, padx=4, pady=6, sticky="NEWS")
        global s
        print hex(id(s)) 

    def networks(self):
        with open("/home/dante/networks.yml", "w") as f:
             f.write("--- #" + "\n")
             f.write("networks:" + "\n")
             print hex(id(s))
             for ent in s:
                 if ent:
                    f.write("- { cloud: cloud1, network: "+str(ent)+ " }"+ "\n")
Now, the file is written just like this, like "s" is not existing:

--- #
I have printed the hex id in "render_network_field" and in "networks" functions and it is seems that the "s" IDs are different. I thought that the "s" has the same value in both functions. That's the reason that I am getting nothing written in the file.

Any idea how I can correct this?

P.S. Please note that I am quite new to programming.

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 468 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 1,449 Mar-10-2021, 04:21 PM
Last Post: Sir
  [Tkinter] tkinter global variable chalao_adda 6 788 Nov-14-2020, 05:37 AM
Last Post: chalao_adda
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 1,016 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  Help with tkinter gui and functions Omer_ 0 360 Sep-22-2020, 11:43 AM
Last Post: Omer_
  [Tkinter] tkinter issue with variables carrying over between functions PengEng 1 681 Apr-06-2020, 06:13 PM
Last Post: deanhystad
  Functions with Tkinter Reldaing 2 1,332 Jan-03-2020, 06:57 PM
Last Post: Reldaing
  def functions in tkinter Fureneshi 5 1,743 Dec-26-2019, 11:34 PM
Last Post: woooee
  Functions running before they should be - Python Tkinter logic error jhf2 2 785 Nov-23-2019, 03:45 PM
Last Post: jhf2
  [Tkinter] Extrakt a Variable from a closed tkinter window hWp 5 1,618 Aug-23-2019, 09:01 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020