-1

I have a issue when I want to populate a entry box using tkinter. My current script are calling another script to get values. Then I want the data to be displayed in a Tkinter entry box.

This is my current code, I am have tried tbName.set("tbName", account.getName()) somethhing like that (with and without the tbName) I have also triws StringVar. I am not sure if I am using it wrong. I have looked at the following site : effbot. I am very new to Python so any help will b apritiated.

from Tkinter import *
from bank import Bank, SavingsAccount


class BankManager(object):

    root = Tk()

    lblName = Label(root, text="Name")
    lblPin = Label(root, text="Pin")
    lblBalance = Label(root, text="Balance R")
    lblStatus = Label(root, text="Status")

    tbName = Entry(root)
    tbPin = Entry(root)
    tbBalance = Entry(root)
    tbStatus = Entry(root)

    def __init__(self):
        self.bank = None
        self.app = None
        self.current_index = 0
        self.create_bank()
        self.populate_gui(self.current_index)

    def new_account(self, event):
        name = self.app.getEntry('name')
        pin = self.app.getEntry('pin')
        balance = self.app.getEntry('balance')
        account = self.bank.get(pin)
        if account:
            self.app.setEntry("status", "Account with pin exists")
        else:
            self.bank.add(SavingsAccount(name, pin, float(balance)))
            self.app.setEntry("status", "New account created")
            self.current_index = self.bank.getPins().index(pin)

    btnNewAcc = Button(root,text="New Account")
    btnNewAcc.bind("<Button>",new_account)

    def update_account(self, event):
        name = self.app.getEntry('name')
        pin = self.app.getEntry('pin')
        balance = self.app.getEntry('balance')
        account = self.bank.get(pin)
        if account:
            account._name = name
            account._balance = balance
            self.app.setEntry("status", "Account updated")
        else:
            self.app.setEntry("status", "Account with pin doesn't exist")

    btnUpdateAcc = Button(root, text="Update Account")
    btnUpdateAcc.bind("<Button>",update_account)

    def remove_account(self, event):
        pin = self.app.getEntry('pin')
        account = self.bank.get(pin)
        if account:
            self.bank.remove(pin)
            self.app.setEntry("status", "Account removed")
            self.current_index = 0
        else:
            self.app.setEntry("status", "Account with pin doesn't exist")

    btnRemoveAcc = Button(root,text="Remove Account")
    btnRemoveAcc.bind("<Button>",remove_account)

    def compute_interest(self, event):
        self.bank.computeInterest()
        pin = self.app.getEntry('pin') 
        account = self.bank.get(pin)
        if account:
            self.app.setEntry("status", "Interest updated")
            self.app.setEntry("balance", str(account.getBalance()))
        else:
            self.app.setEntry("status", "Account with pin doesn't exist")

    btnConputeInterest = Button(root,text="Compute Interest")
    btnConputeInterest.bind("<Button>",compute_interest)

    def press_navigator(self, event):
        if button == "Previous":
            if self.current_index == 0:
                self.current_index = len(self.bank.getPins()) - 1
            else:
                self.current_index -= 1
        elif button == "Next":
            if self.current_index == len(self.bank.getPins()) - 1:
                self.current_index = 0
            else:
                self.current_index += 1
        self.populate_gui(self.current_index)

    btnPrevious = Button(root,text="Previous")
    btnPrevious.bind("<Button>",press_navigator)

    btnNext = Button(root,text="Next")
    btnNext.bind("<Button>",press_navigator)

    lblName.grid(row=0)
    lblPin.grid(row=1)
    lblBalance.grid(row=2)
    lblStatus.grid(row=3)

    tbName.grid(row=0, column=1)
    tbPin.grid(row=1, column=1)
    tbBalance.grid(row=2, column=1)
    tbStatus.grid(row=3, column=1)

    btnNewAcc.grid(row=0, column=2)
    btnUpdateAcc.grid(row=1, column=2)
    btnRemoveAcc.grid(row=2, column=2)
    btnConputeInterest.grid(row=3, column=2)
    btnPrevious.grid(row=4, column=0)
    btnNext.grid(row=4, column=1)

    root.mainloop()

    def create_bank(self):
        self.bank = Bank()
        a1 = SavingsAccount('zzz', '111', 100)
        a2 = SavingsAccount('yyy', '222', 200)
        a3 = SavingsAccount('xxx', '333', 300)
        self.bank.add(a1)
        self.bank.add(a3)
        self.bank.add(a2)

    def populate_gui(self, index):
        account = self.bank.get(self.bank.getPins()[index])
        tbName.set("tbName", account.getName())    

if __name__ == '__main__':
    BankManager()
Ruan
  • 397
  • 1
  • 6
  • 17
  • Are you sure you are getting something from `account.getName()`? Also, are you getting any error messages? – Lafexlos Mar 21 '17 at 06:43
  • @lafexlos: When I created the app using appJar the data come through `self.app.setEntry("name")` . I want to do this using tkinter. No errors so far. I just want the values to be in the entry box so that I can carry on with the buttons ect... – Ruan Mar 21 '17 at 06:46
  • 1
    Entry has no `set` method. Any tkinter tutorial should cover how to set value of an Entry widget. Even the page you linked shows two types of _changing text of Entry_. – Lafexlos Mar 21 '17 at 06:49
  • 1
    [How to set default text for a Tkinter Entry widget](http://stackoverflow.com/questions/20125967/how-to-set-default-text-for-a-tkinter-entry-widget) This one should help. – Lafexlos Mar 21 '17 at 06:51
  • @lafexlos: The `v.set("a default value")` and `s = v.get()` from the site. But not sure if I am using them incorrect or something. I tried something like `bName.set("tbName", account.getName())'. Not sure if I am doing it wrong. – Ruan Mar 21 '17 at 06:52
  • @lafexlos if i put `v = StringVar(root, value='default text') tbName = Entry(root, textvariable=v)` above the `__init__` it works fine, but if i put it in my `populate_gui` nothing happens, even a default value does not work. So it looks like its not being executed even though it is stated in my `__init__` – Ruan Mar 21 '17 at 07:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138595/discussion-between-ruan-and-lafexlos). – Ruan Mar 21 '17 at 07:07
  • 1
    Possible duplicate of [How to set default text for a Tkinter Entry widget](https://stackoverflow.com/questions/20125967/how-to-set-default-text-for-a-tkinter-entry-widget) – Stevoisiak Feb 15 '18 at 14:17

1 Answers1

1

if you want to set text in the Entry widget, you need to use Entry.insert(0,'text') instead of v.set. The 0 in the insert method is the place you want to insert the text, in this case it is in the beginning of the Entry.

If you don't want the user to edit what is inside the Entry, you can use Entry.config(state='readonly'); if you want to edit the content later you need to call Entry.config(state=NORMAL) before you can use Entry.insert again. You don't need to set StringVar to the Entry, only using Entry.get() is sufficient.

Mia
  • 2,466
  • 22
  • 38