Мне нужно открыть второе окно от первого, потому что так работает приложение.
Мне также нужно открыть второе окно независимо от первого, т. е. само по себе, чтобы я мог проводить модульное тестирование по мере его разработки.
Этот вопрос уничтожает предыдущее окно, поскольку открывается второе окно. Не то, что я хочу: Python tkinter с несколькими окнами
Мой предыдущий вопрос закрывает первое окно при открытии второго окна. Не то, что я хочу. Хотя в комментариях указывается на опечатку (опущена ()), в принятом ответе используются методы вывода и деиконификации, которые в конечном итоге будут полезны, но не являются решением этой проблемы. Python tkinter закрывает первое окно при открытии второго окна
Это самый близкий вариант, поскольку он открывает второе окно из первого окна, но не касается обоих (i) открытия второго окна из отдельный файл и (ii) также возможность открывать второе окно независимо: несколько окон класса Python tkinter
Вот ВТОРОЕ окно в файле с именем Location.py и он открывается нормально независимо:
Код: Выделить всё
import tkinter as tk
from tkinter import ttk
class Location(tk.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
root.title("Location")
root.geometry('400x275')
# Frames
self.mainframe = ttk.Frame(self.master)
self.mainframe.grid(column = 0, row=0)
ttk.Label(self.mainframe, text = "Second Window").grid(column=1, row=1, sticky=(tk.W, tk.E))
if __name__ == "__main__":
root = tk.Tk()
Location(root)
root.mainloop()
Код: Выделить всё
import tkinter as tk
from tkinter import ttk
import Location
class Building_Info(tk.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Frames
self.infoframe = ttk.Frame(self.master, height=400, width=200, padding="50 100 50 100", borderwidth=10)
self.infoframe['relief'] = 'raised'
self.infoframe.grid(column = 0, row=0, sticky=(tk.E, tk.N))
self.buttonframe = ttk.Frame(self.master, height=400, width=200, padding="50 100 50 100", borderwidth=10)
self.buttonframe['relief'] = 'raised'
self.buttonframe.grid(column = 1, row=0, sticky=(tk.E, tk.N))
# BUTTONS
confirm_button = ttk.Button(self.infoframe, text = 'Stakeholders', command = self.open_location)
confirm_button.grid(column=0, row=2)
confirm_button = ttk.Button(self.buttonframe, text = 'Location', command = self.open_location)
confirm_button.grid(column=0, row=2)
for child in self.infoframe.winfo_children():
child.grid_configure(padx=5, pady=5)
for child in self.buttonframe.winfo_children():
child.grid_configure(padx=5, pady=5)
# METHODS
def open_location(self):
Location.Location()
if __name__ == "__main__":
root = tk.Tk()
root.title("Building Information")
root.geometry('600x400')
Building_Info(root)
root.mainloop()
Код: Выделить всё
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "c:\Users\User\Documents\Python\Tutorials\BuildingInfo_Stay_Open.py", line 32, in open_location
Location_Stay_Open.Location()
File "c:\Users\User\Documents\Python\Tutorials\Location_Stay_Open.py", line 9, in __init__
root.title("Location")
^^^^
NameError: name 'root' is not defined
Код: Выделить всё
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "c:\Users\User\Documents\Python\Tutorials\BuildingInfo_Stay_Open.py", line 32, in open_location
Location_Stay_Open.Location(self.root)
^^^^^^^^^
AttributeError: 'Building_Info' object has no attribute 'root'. Did you mean: '_root'?
Код: Выделить всё
Traceback (most recent call last):
File "c:\Users\User\Documents\Python\Tutorials\Location_Stay_Open.py", line 29, in
Location(root)
File "c:\Users\User\Documents\Python\Tutorials\Location_Stay_Open.py", line 7, in __init__
super().__init__(*args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\tkinter\__init__.py", line 2326, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: create() argument 1 must be str or None, not Tk
Что мне нужно сделать, чтобы второе окно (Местоположение) (i) открывалось независимо и (ii) открывалось из первого окна?
Подробнее здесь: https://stackoverflow.com/questions/785 ... rate-files