Python Forum

Full Version: changing and getting value through button
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I have two moduls named First:
import  tkinter as tk

from Second import ManualNodes

class CC:
    def __init__(self):
        root = tk.Tk()
        root.minsize(300, 300)
        tmp = ManualNodes(4, root=root)
        print(tmp.get_forc())
        root.mainloop()

x=CC()
and Second:
import numpy as np
import  tkinter as tk


class ManualNodes:

    def __init__(self, row_num, root):
        self.row_num = row_num
        self.forces_table = np.zeros([self.row_num, 3])
        self.root = root
        self.toplevel_dialog = tk.Toplevel(self.root)
        self.toplevel_dialog.transient(self.root)
        self.tkinter_menu()

    def get_forc(self):
        return self.forces_table


    def tkinter_menu(self):
        n = 0
        lbx = tk.Label(master=self.toplevel_dialog, text='X forces')
        lbx.grid(row=0, column=1)
        lby = tk.Label(master=self.toplevel_dialog, text='Y forces')
        lby.grid(row=0, column=2)
        lbz = tk.Label(master=self.toplevel_dialog, text='Z forces')
        lbz.grid(row=0, column=3)
        mrow = None
        for mrow in range(self.row_num):
            lb = tk.Label(master=self.toplevel_dialog, text='węzeł numer {}'.format(n))
            lb.grid(row=n + 1, column=0)
            n += 1
            for mcol in range(3):
                en = tk.Entry(master=self.toplevel_dialog)
                en.grid(row=mrow + 1, column=mcol + 1)
                en.insert(0, '0')
        accept = tk.Button(master=self.toplevel_dialog, text="accept", command=self.accept_but)
        accept.grid(row=mrow + 2, column=0, columnspan=4)


    def accept_but(self):
        for key in self.toplevel_dialog.children:
            if str(type(self.toplevel_dialog.children[key])) == '<class \'tkinter.Entry\'>':
                row = self.toplevel_dialog.children[key].grid_info()['row']
                col = self.toplevel_dialog.children[key].grid_info()['column']
                self.forces_table[row - 1][col - 1] = float(self.toplevel_dialog.children[key].get())
        self.toplevel_dialog.destroy()
when I First i get output:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
I need that tmp.get return value wchich i input through accept button. If possible i prefer not to edit First script.
First is the errant code. The buttons don't appear until you call "root.mainloop()". Printing tmp.get_force() before this means you are printing the initial values. You need to wait until after you press the "Accept" button to print the modified variables.

Unfortunately the only way you can do the printing from First is to wait until the the root window is closed. This ends root.mainloop()
root = tk.Tk()
root.minsize(300, 300)
tmp = ManualNodes(4, root=root)
# print(tmp.get_forc())  Window not visible yet
root.mainloop()
print(tmp.get_forc())
Ok, thanks for help and quick answer Smile