Python - pull things from the bottom of the code
Question: I have this code:

    
    
    import fcntl, socket, struct
    import base64
    import time, datetime
    import netifaces
    from Tkinter import *
    
    def getHwAddr(ifname):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
        return '-'.join(['%02x' % ord(char) for char in info[18:24]])
    
    tvip = "10.0.1.2"
    tvappstring = "UE55C8000"
    myip = netifaces.ifaddresses(netifaces.gateways()['default'][netifaces.AF_INET][1])[netifaces.AF_INET][0]['addr']
    mymac = getHwAddr(netifaces.gateways()['default'][netifaces.AF_INET][1])
    appstring = "Pepin's Samsung TV Remote"
    remotename = "Pepin's Samsung TV Remote"
    
    def sendKey(skey, dataSock, appstring):
     messagepart3 = chr(0x00) + chr(0x00) + chr(0x00) + chr(len(
    base64.b64encode(skey))) + chr(0x00) + base64.b64encode(skey);
     part3 = chr(0x00) + chr(len(appstring)) + chr(0x00) \
    + appstring + chr(len(messagepart3)) + chr(0x00) + messagepart3
     dataSock.send(part3);
    
    def sendText(stext, dataSock, appstring):
     messagepart3 = chr(0x00) + chr(0x00) + chr(0x00) + chr(len(
    base64.b64encode(stext))) + chr(0x00) + base64.b64encode(stext);
     part3 = chr(0x00) + chr(len(appstring)) + chr(0x00) \
    + appstring + chr(len(messagepart3)) + chr(0x00) + messagepart3
     dataSock.send(part3); 
    
    root = Tk()
    root.title("Pepin's Samsung TV Remote")
    root.geometry("391x595")
    
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((tvip, 55000)) 
    ipencoded = base64.b64encode(myip)
    macencoded = base64.b64encode(mymac)
    messagepart1 = chr(0x64) + chr(0x00) + chr(len(ipencoded)) \
    + chr(0x00) + ipencoded + chr(len(macencoded)) + chr(0x00) \
    + macencoded + chr(len(base64.b64encode(remotename))) + chr(0x00) \
    + base64.b64encode(remotename)
    
    part1 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
    + chr(len(messagepart1)) + chr(0x00) + messagepart1
    sock.send(part1)
    
    messagepart2 = chr(0xc8) + chr(0x00)
    part2 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
    + chr(len(messagepart2)) + chr(0x00) + messagepart2
    sock.send(part2);
    
    class Application():
        """Pepin's Samsung TV Remote"""
    
        def __init__(self, master):
            self.master = master
            self.create_widgets()
    
        def create_widgets(self):
    
            btn_key_poweroff = Button(self.master, text = "Power", bg="red", width=4, height=2, command = lambda: sendKey("KEY_POWEROFF", sock, tvappstring)) 
    
            entry_send_custom = Entry(self.master, width=22)
            entry_send_custom.grid(row=1, column=3, columnspan=4, padx=(15,0), ipady=8)
    
            btn_send_custom = Button(self.master, text = "SEND CUSTOM KEY", width=19, height=2, command = lambda: sendText(entry_send_custom.get(), sock, tvappstring)) 
                btn_send_custom.grid(row=2, column=3, columnspan=5, padx=(15,0))
    
            label_tvip = Label(self.master, text="TV IP:")
            label_tvip.grid(row=3, column=3, columnspan=1, padx=(15,0), ipady=8)
    
            self.entry_tvip = Entry(self.master, width=16)
            self.entry_tvip.grid(row=3, column=4, columnspan=3, ipady=8)
    
            label_tvappstring = Label(self.master, text="TV MODEL (Tv App String)")
            label_tvappstring.grid(row=4, column=3, columnspan=4, padx=(15,0), ipady=8, sticky=W)
    
            self.entry_tvappstring = Entry(self.master, width=22)
            self.entry_tvappstring.grid(row=5, column=3, columnspan=4, padx=(15,0), ipady=8)
            self.entry_tvappstring.insert(0, "UE55C8000")
    
            btn_connect = Button(self.master, text = "CONNECT TO TV", width=19, height=2, command = lambda: self.Connection()) 
                btn_connect.grid(row=6, column=3, columnspan=5, padx=(15,0))     
    
    app = Application(root)
    root.mainloop()
    
    
    sock.close()
    

But I want to use `entry_tvip.get()` in top of the code.

Is there simple way to do it?

And start this section of the code:

    
    
    sock.connect((tvip, 55000)) 
    ipencoded = base64.b64encode(myip)
    macencoded = base64.b64encode(mymac)
    messagepart1 = chr(0x64) + chr(0x00) + chr(len(ipencoded)) \
    + chr(0x00) + ipencoded + chr(len(macencoded)) + chr(0x00) \
    + macencoded + chr(len(base64.b64encode(remotename))) + chr(0x00) \
    + base64.b64encode(remotename)
    
    part1 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
    + chr(len(messagepart1)) + chr(0x00) + messagepart1
    sock.send(part1)
    
    messagepart2 = chr(0xc8) + chr(0x00)
    part2 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
    + chr(len(messagepart2)) + chr(0x00) + messagepart2
    sock.send(part2);
    

After pushing button. Is there way to do it too?

Thanks! I'm new to Python


Answer: Since `entry_tvip` is a method of the `Application` class and `app` is an
instance of this class, you can call `app.entry_tvip.get()`

You will not be able to do this anywhere before the `app` is created, so I
would rethink your approach.

