gpt4 book ai didi

python - 将两个窗口结果放入具有两个选项卡的窗口中

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:04 26 4
gpt4 key购买 nike

起始页有两个按钮。这些按钮显示两种不同的结果。弹出两个窗口来查看结果似乎太拥挤了。我希望通过单击第一个窗口中的单个按钮将结果显示在带有两个选项卡的窗口中(两个选项卡指的是按钮中的两个结果)。我怎样才能做到这一点?

下面的代码是可执行的。

import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk

all_figures1 = []
selected_figure1 = 0
all_figures2 = []
selected_figure2 = 0


class MyClass1():

def __init__(self):

self.sheets = [[1,2,3], [3,1,2], [1,5,1]]
self.W = 2
self.L = 5


def plot_sheet(self, data):
"""plot single figure"""

fig, ax = plt.subplots(1)

ax.set_xlim([0, self.W])
ax.set_ylim([0, self.L])

ax.plot(data)

return fig

def generate_all_figures(self):
"""create all figures and keep them on list"""
global all_figures

for data in self.sheets:
fig = self.plot_sheet(data)
all_figures1.append(fig)


class MyClass2():

def __init__(self):

self.sheets = [[3,2,1], [2,5,3], [5,1,5]]
self.W = 2
self.L = 5


def plot_sheet(self, data):
"""plot single figure"""

fig, ax = plt.subplots(1)

ax.set_xlim([0, self.W])
ax.set_ylim([0, self.L])

ax.plot(data)

return fig

def generate_all_figures(self):
"""create all figures and keep them on list"""
global all_figures

for data in self.sheets:
fig = self.plot_sheet(data)
all_figures2.append(fig)


dataPlot1 = None
dataPlot2 = None

def on_window1():

def show_figure(number):
global dataPlot1

current_num = str(number+1)
total_num = str(len(all_figures1))



counter = tk.Label(top, text = current_num + "/" + total_num)
counter.grid(row=4,column=0)



# remove old canvas
if dataPlot1 is not None: # at start there is no canvas to destroy
dataPlot1.get_tk_widget().destroy()

# get figure from list
one_figure = all_figures1[number]

# display canvas with figuere
dataPlot = FigureCanvasTkAgg(one_figure, master=top)
dataPlot.draw()
dataPlot.get_tk_widget().grid(row=0, column=0)

toolbar_frame = tk.Frame(top)
toolbar_frame.grid(row=1,column=0)
toolbar = NavigationToolbar2Tk(dataPlot, toolbar_frame)
toolbar.update()

def on_prev():
global selected_figure1

# get number of previous figure
selected_figure1 -= 1
if selected_figure1 < 0:
selected_figure1 = len(all_figures1)-1

show_figure(selected_figure1)

def on_next():
global selected_figure1

# get number of next figure
selected_figure1 += 1
if selected_figure1 > len(all_figures1)-1:
selected_figure1 = 0

show_figure(selected_figure1)

top = tk.Toplevel()
top.wm_geometry("794x370")
top.title('Optimized Map 1')

selected_figure1 = 0
dataPlot = None # default value for `show_figure`
show_figure(selected_figure1)

frame = tk.Frame(top)
frame.grid(row=3, column=0)

b1 = tk.Button(frame, text="<<", command=on_prev)
b1.grid(row=2, column=0)

b2 = tk.Button(frame, text=">>", command=on_next)
b2.grid(row=2, column=1)



def on_window2():

def show_figure(number):
global dataPlot2

current_num = str(number+1)
total_num = str(len(all_figures2))


counter = tk.Label(top, text = current_num + "/" + total_num)
counter.grid(row=4,column=0)



# remove old canvas
if dataPlot2 is not None: # at start there is no canvas to destroy
dataPlot2.get_tk_widget().destroy()

# get figure from list
one_figure = all_figures2[number]

# display canvas with figuere
dataPlot = FigureCanvasTkAgg(one_figure, master=top)
dataPlot.draw()
dataPlot.get_tk_widget().grid(row=0, column=0)

toolbar_frame = tk.Frame(top)
toolbar_frame.grid(row=1,column=0)
toolbar = NavigationToolbar2Tk(dataPlot, toolbar_frame)
toolbar.update()

def on_prev():
global selected_figure2

# get number of previous figure
selected_figure2 -= 1
if selected_figure2 < 0:
selected_figure2 = len(all_figures2)-1

show_figure(selected_figure2)

def on_next():
global selected_figure2

# get number of next figure
selected_figure2 += 1
if selected_figure2 > len(all_figures2)-1:
selected_figure2 = 0

show_figure(selected_figure2)

top = tk.Toplevel()
top.wm_geometry("794x370")
top.title('Optimized Map2')

selected_figure2 = 0
dataPlot = None # default value for `show_figure`
show_figure(selected_figure2)

frame = tk.Frame(top)
frame.grid(row=3, column=0)

b1 = tk.Button(frame, text="<<", command=on_prev)
b1.grid(row=2, column=0)

b2 = tk.Button(frame, text=">>", command=on_next)
b2.grid(row=2, column=1)

window = tk.Tk()

mc1 = MyClass1()
mc1.generate_all_figures()

mc2 = MyClass2()
mc2.generate_all_figures()

b1 = tk.Button(window, text="Next", command=on_window1)
b1.grid(row=0, column=0)

b2 = tk.Button(window, text="Next", command=on_window2)
b2.grid(row=1, column=0)

window.mainloop()

最佳答案

您应该添加一个类似于以下内容的函数:

def switch_tabs(clicked):
global active
tabs[active].grid_forget()
tabs[clicked].grid()
active = clicked

其中 tabs 可以是框架键的列表或字典,active 是表示事件选项卡的全局变量,clicked 是代表被单击的选项卡的键。框架本身需要一个共同的父级 - 就像主窗口一样。 Here's some documentationgrid_forget上。

然后,当您创建按钮时,您可以将此函数包装在直接指向其选项卡的 lambda 中。您可能希望将按钮本身放在与两个选项卡的公共(public)父级相同的框架上。

b1 = tk.Button(window, text="<<", command=lambda: switch_tabs(tab1))

在这种情况下,tab1 可以是一个字符串或整数,它与您想要通过按下按钮切换到的选项卡相关联。例如,如果您使用列表来存储帧,则 tab1 可能为 0;如果您使用列表,则可能为 'tab1'正在使用字典。

在这里,我还使用了lambda expression作为定义新函数的快速而简单的方法。我可以很容易地写出类似的东西

def switch_to_tab1():
switch_tabs(tab1)
b1 = tk.Button(window, text="<<", command=switch_to_tab1)

关于python - 将两个窗口结果放入具有两个选项卡的窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57743778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com