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
#1
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)
        self.create_widgets()

    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)
        start_button.pack()
        new_network_button.pack()
        next_button.pack()
        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)})
        self.master.switch_frame(Networks)

    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")
        entry_field.pack()
        global s
        s=entry_field.get()  
        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:

--- #
networks:
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,329 Jan-24-2024, 06:44 AM
Last Post: Liliana
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,202 Oct-15-2021, 08:01 AM
Last Post: drSlump
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,649 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,637 Mar-10-2021, 04:21 PM
Last Post: Sir
  [Tkinter] tkinter global variable chalao_adda 6 11,033 Nov-14-2020, 05:37 AM
Last Post: chalao_adda
  Help with tkinter gui and functions Omer_ 0 1,514 Sep-22-2020, 11:43 AM
Last Post: Omer_
  [Tkinter] tkinter issue with variables carrying over between functions PengEng 1 1,739 Apr-06-2020, 06:13 PM
Last Post: deanhystad
  Functions with Tkinter Reldaing 2 2,561 Jan-03-2020, 06:57 PM
Last Post: Reldaing
  def functions in tkinter Fureneshi 5 7,555 Dec-26-2019, 11:34 PM
Last Post: woooee
  Functions running before they should be - Python Tkinter logic error jhf2 2 2,126 Nov-23-2019, 03:45 PM
Last Post: jhf2

Forum Jump:

User Panel Messages

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