Python Forum
[Tkinter] changing and getting value through button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] changing and getting value through button
#1
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.
Reply
#2
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())
Reply
#3
Ok, thanks for help and quick answer Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Trying to change font size w/o changing button size python63 3 9,739 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  [Tkinter] changing background color of a button ieee488 2 4,165 Sep-06-2019, 01:24 PM
Last Post: ieee488
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,951 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Error while changing button relief from raised to sunken on left click mgtheboss 1 5,295 Jan-09-2018, 06:05 PM
Last Post: Gribouillis
  [Tkinter] Problem with changing label text on button press xk2006x 1 5,548 Jun-02-2017, 06:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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