Home>


There are two files, A.py and B.py.
First, start A and put the value in the input box ⇒
When the button below is pressed, the window set in B will be launched ⇒
Create input box in window B ⇒
I want to display the value entered in the A input box inside the B input box

I want to do something like

. How can I write it?
What should I do because the B side input box is not displayed and there is no error? .

If i know, can you professor?

A

from tkinter import *
from tkinter import ttk
import B
def execute_py1 ():
    B.sub_window ()
root = Tk ()
root.geometry ("80x80 + 10 + 10")
entry1 = ttk.Entry (width = 10)
entry1.pack (fill = 'x', padx = 5, pady = 5)
button1 = ttk.Button (root, text = 'button', padding = 3, command = execute_py1)
button1.pack (fill = 'x', padx = 5, pady = 5)
root.mainloop ()

B

from tkinter import *
from tkinter import ttk
def sub_window ():
    sub_window1 = Toplevel ()
    sub_window1.geometry ("100x100 + 160 + 10")
    entry1 = ttk.Entry (width = 10)
    entry1.pack (fill = 'x', padx = 5, pady = 5)
  • Answer # 1

    A relatively simple implementation

    B.py

    from tkinter import *
    from tkinter import ttk
    def sub_window (value = None):
        sub_window1 = Toplevel ()
        sub_window1.geometry ("100x100 + 160 + 10")
        entry1 = ttk.Entry (sub_window1, width = 10)
        if value is not None:
            entry1.insert (0, value)
        entry1.pack (fill = 'x', padx = 5, pady = 5)


    If you pass an argument (value) to thesub_window ()function like this, modify it so that it is displayed onEntry
    When calling this function inA.py

    def execute_py1 ():
        B.sub_window (entry1.get ())


    I think that it is good to remodel so that the contents of Entry are passed as parameters.

  • Answer # 2

    (deleted due to duplication)

  • Answer # 3

    B.sub_window (value)

    It needs to be modified to pass a value as an argument as in

    .
    I need root too.

  • Answer # 4

    A.py

    from tkinter import *
    from tkinter import ttk
    import tkinter as tk
    import B
    class Main_Proc:
        def execute_B (self):
            self.newWindow = Tk ()
            parm = self.entry1.get ()
            self.app = B.Sub_Proc (self.newWindow, parm)
        def __init __ (self, master):
            self.master = master
            self.master.geometry ("80x80 + 10 + 10")
            self.frame = tk.Frame (self.master)
            self.entry1 = ttk.Entry (width = 10)
            self.entry1.pack (fill = 'x', padx = 5, pady = 5)
            self.button1 = ttk.Button (self.frame, text = 'button', padding = 3, command = self.execute_B)
            self.button1.pack (fill = 'x', padx = 5, pady = 5)
            self.frame.pack ()
    def main ():
        root = tk.Tk ()
        app = Main_Proc (root)
        root.mainloop ()
    if __name__ == '__main__':
        main ()

    B.py

    from tkinter import *
    from tkinter import ttk
    import tkinter as tk
    class Sub_Proc ():
        def __init __ (self, master, parm):
            master.geometry ("100x100 + 160 + 10")
            self.entry1 = ttk.Entry (master, width = 10)
            self.entry1.pack (fill = 'x', padx = 5, pady = 5)
            self.entry1.delete (0, tk.END)
            self.entry1.insert (tk.END, parm)