How can I pass data from one Tkinter window to another?

Asked

Viewed 79 times

0

I have two Tkinter Windows, and I want to pass the dice from one window to the other window.

inserir a descrição da imagem aqui

Code from the first Window:

from tkinter import *
from get_pos_mouse import Get_Mouse_Pos


class Application():
    def __init__(self, master=None):

        self.master = master

        self.bt = Button(self.master, text='+')
        self.bt.bind("<ButtonRelease-1>", self.new_window)
        self.bt.pack()

        self.lb = Label(self.master, text='Pos Here:')
        self.lb.pack()

    def new_window(self, event=None):
        self.new_window = Toplevel(self.master)
        self.app = Get_Mouse_Pos(self.new_window)
   
    def change_lb(self, pos):
        self.lb.config(text=pos)




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

Code of the second Window:

import pyautogui
from tkinter import *

class Get_Mouse_Pos():
    def __init__(self, master):
        self.master = master

        self.master.overrideredirect(1)
        self.master.wm_attributes('-transparentcolor', 'yellow')
        self.master.attributes("-topmost", True)

        self.master.bind("<Escape>", self.safe_quit)

        self.screenWidth, self.screenHeight = pyautogui.size()

        self.canvas_total = Canvas(
            self.master, width=self.screenWidth, height=self.screenHeight, bg='yellow')
        self.canvas_total.bind("<Button-1>", self.get_pos_mouse)
        self.canvas_total.pack()

        self.draw_lines()

    def draw_lines(self):
        x, y = pyautogui.position()

        self.canvas_total.delete("all")

        self.canvas_total.create_line(
            0, y, self.screenWidth, y, fill="red", width=2)

        self.canvas_total.create_line(
            x, 0, x, self.screenHeight, fill="red", width=2)

        self.master.after(20, self.draw_lines)

    def get_pos_mouse(self, event=None):

        pos = (event.x, event.y)
        self.safe_quit()

    def safe_quit(self, event=None):
        self.master.destroy()

The goal was that when the "get_pos_mouse()" function of the second window was called the "change_lb()" function of the first window was also called.

1 answer

0

If you want to relate two objects, the most appropriate would be to instantiate that object within your main class.

Using inheritance, class property, you can call the methods of the class you are wanting to use.

In this case, write: Application(Get_Mouse_Pos): So he will present this class as mother/father of the App class And consequently you will inherit her methods.

The only specific annotation is that you should instantiate this class, so call it with this syntax Get_Mouse_Pos.__init__(self, masterqueforusar), or use the classic variable instancement self.nome = Get_Mouse_Pos, the latter does not need the inheritance, but will always need to call the instance to get the class methods.

Without these two methods, you won’t be able to access the methods within the Get_mouse_pos class at all, because python doesn’t know you’re going to use it, or how you’re going to use it, so you need to indicate that you’re taking methods from the class you want, in that case, the Get_mouse_pos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.