Bean
Bean

Reputation: 1

Tkinter not saving entry as variable

I've tried looking at other peoples code snippets online, tried looking at previous solutions and NOTHING WORKS. I need to save the fee entry as a variable when the button is clicked.

Here's all the code:

import tkinter as tk
import tkinter.font as tkFont
import requests
from bs4 import BeautifulSoup
import os
from bitcoin import *
with open ("wallet.dat", "r") as file:
    priv=file.read()
my_addr = privtoaddr(priv)

url = 'https://www.blockchain.com/btc/address/1DswsKETdbXkrw5Br1Tbvu6hCbpQ7QCRc7'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
balance = soup.body.find(class_="sc-1ryi78w-0 cILyoi sc-16b9dsl-1 ZwupP u3ufsr-0 eQTRKC")
print(balance)

class App:
    def __init__(self, root):
        #setting title
        root.title("BeanWallet | Send")
        #setting window size
        width=354
        height=246
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(alignstr)
        root.resizable(width=False, height=False)

        GLabel_764=tk.Label(root)
        ft = tkFont.Font(family='Times',size=16)
        GLabel_764["font"] = ft
        GLabel_764["fg"] = "#333333"
        GLabel_764["justify"] = "center"
        GLabel_764["text"] = "BeanWallet"
        GLabel_764.place(x=0,y=0,width=105,height=41)

        GButton_815=tk.Button(root)
        GButton_815["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        GButton_815["font"] = ft
        GButton_815["fg"] = "#000000"
        GButton_815["justify"] = "center"
        GButton_815["text"] = "Home"
        GButton_815.place(x=110,y=10,width=55,height=23)
        GButton_815["command"] = self.GButton_815_command

        GButton_888=tk.Button(root)
        GButton_888["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        GButton_888["font"] = ft
        GButton_888["fg"] = "#6a6969"
        GButton_888["justify"] = "center"
        GButton_888["text"] = "Send"
        GButton_888.place(x=170,y=10,width=55,height=23)
        GButton_888["command"] = self.GButton_888_command

        GButton_30=tk.Button(root)
        GButton_30["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        GButton_30["font"] = ft
        GButton_30["fg"] = "#000000"
        GButton_30["justify"] = "center"
        GButton_30["text"] = "Receive"
        GButton_30.place(x=230,y=10,width=55,height=23)
        GButton_30["command"] = self.GButton_30_command

        GButton_458=tk.Button(root)
        GButton_458["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        GButton_458["font"] = ft
        GButton_458["fg"] = "#000000"
        GButton_458["justify"] = "center"
        GButton_458["text"] = "Settings"
        GButton_458.place(x=290,y=10,width=55,height=23)
        GButton_458["command"] = self.GButton_458_command

        GLabel_84=tk.Label(root)
        ft = tkFont.Font(family='Times',size=10)
        GLabel_84["font"] = ft
        GLabel_84["fg"] = "#333333"
        GLabel_84["justify"] = "center"
        GLabel_84["text"] = "Send To:"
        GLabel_84.place(x=0,y=40,width=352,height=25)

        GLineEdit_642=tk.Entry(root)
        GLineEdit_642["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=10)
        GLineEdit_642["font"] = ft
        GLineEdit_642["fg"] = "#333333"
        GLineEdit_642["justify"] = "center"
        GLineEdit_642["text"] = "Address"
        GLineEdit_642.place(x=0,y=60,width=352,height=30)

        GLabel_227=tk.Label(root)
        ft = tkFont.Font(family='Times',size=10)
        GLabel_227["font"] = ft
        GLabel_227["fg"] = "#333333"
        GLabel_227["justify"] = "center"
        GLabel_227["text"] = "Amount:"
        GLabel_227.place(x=0,y=90,width=353,height=25)

        GLineEdit_37=tk.Entry(root)
        GLineEdit_37["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=10)
        GLineEdit_37["font"] = ft
        GLineEdit_37["fg"] = "#333333"
        GLineEdit_37["justify"] = "center"
        GLineEdit_37["text"] = "0.0001"
        GLineEdit_37.place(x=0,y=110,width=352,height=30)

        GLabel_669=tk.Label(root)
        ft = tkFont.Font(family='Times',size=10)
        GLabel_669["font"] = ft
        GLabel_669["fg"] = "#333333"
        GLabel_669["justify"] = "center"
        GLabel_669["text"] = "Fee (sat/vB)"
        GLabel_669.place(x=0,y=140,width=102,height=30)

        fee = tk.IntVar()
        GLineEdit_932=tk.Entry(root, textvariable=fee)
        GLineEdit_932.focus()
        GLineEdit_932.pack()
        GLineEdit_932["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=10)
        GLineEdit_932["font"] = ft
        GLineEdit_932["fg"] = "#333333"
        GLineEdit_932["justify"] = "center"
        GLineEdit_932["text"] = "Fee"
        GLineEdit_932.place(x=0,y=170,width=101,height=30)

        GButton_254=tk.Button(root)
        GButton_254["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        GButton_254["font"] = ft
        GButton_254["fg"] = "#000000"
        GButton_254["justify"] = "center"
        GButton_254["text"] = "Send"
        GButton_254.place(x=250,y=150,width=90,height=50)
        GButton_254["command"] = self.GButton_254_command

        GLabel_60=tk.Label(root)
        ft = tkFont.Font(family='Times',size=10)
        GLabel_60["font"] = ft
        GLabel_60["fg"] = "#333333"
        GLabel_60["justify"] = "center"
        GLabel_60["text"] = "Balance displayed in console."
        GLabel_60.place(x=0,y=210,width=353,height=30)

    def GButton_815_command(self):
        root.destroy()
        mypath = os.path.abspath(os.path.dirname(__file__))
        os.system( f'"{mypath}/home.py"' )


    def GButton_888_command(self):
        print("command")


    def GButton_30_command(self):
        root.destroy()
        mypath = os.path.abspath(os.path.dirname(__file__))
        os.system( f'"{mypath}/receive.py"' )


    def GButton_458_command(self):
        root.destroy()
        mypath = os.path.abspath(os.path.dirname(__file__))
        os.system( f'"{mypath}/settings.py"' )

    def GButton_254_command(self):
        fees = self.fee.get()
        print(fees)


if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()

I get this error code no matter how much I change the code up:

Traceback (most recent call last):
  File "C:\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:\Users\ThaSq\Downloads\BTCWEB\BeanWallet\remastered\send.py", line 172, in GButton_254_command
    fees = self.fee.get()
AttributeError: 'App' object has no attribute 'fee'

I've tried using GLineEdit_932.get() and that doesn't work either. I've also tried making the variable a global variable, with no success.

Upvotes: 0

Views: 492

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54635

Of course you do, because you are trying to USE the variable before you have CREATED it. But in this case, moving those lines isn't the right answer. You need that fetching code to be in a separate function. SO, for example:

def create_ui():
    def get_input():
        fees = fee.get()
        print(fees)

    fee = tk.IntVar()
    GLineEdit_932=tk.Entry(root, textvariable=fee)
    GLineEdit_932.focus()
    ...

Now you can call create_ui() to do the one-time initialization, but your button callback can call the little get_input function to fetch the values.

Followup

Having seen your full code, there is a better way.

class App:
    def __init__(self, root):
        #setting title
        ...
        self.fee = tk.IntVar()
        GLineEdit_932=tk.Entry(root, textvariable=self.fee)
        GLineEdit_932.focus()
        ...
        GButton_254.place(x=250,y=150,width=90,height=50)
        GButton_254["command"] = self.get_input
        ...
        GLabel_60["text"] = "Balance displayed in console."
        GLabel_60.place(x=0,y=210,width=353,height=30)

    def get_input(self):
        fees = self.fee.get()
        print(fees)

You can see that your sample is doing this kind of thing for the other buttons. Follow the pattern.

Upvotes: 1

Related Questions