简体   繁体   English

有没有办法在 jupyter 笔记本 GUI 文本区域中显示 python 文件结果?

[英]Is there a way to show a python file result in a jupyter notebook GUI textarea?

So I am new to working with Jupyter Notebook and tkinter, I am currently working on a python code in Jupyter Notebook that will run a python file and display the results in a text area in a window (Graphical User Interface) I've created. So I am new to working with Jupyter Notebook and tkinter, I am currently working on a python code in Jupyter Notebook that will run a python file and display the results in a text area in a window (Graphical User Interface) I've created. Any help on how to get the python file output in the text area and not in a jupyter notebook cell?有关如何在文本区域而不是在 jupyter 笔记本单元格中获取 python 文件 output 的任何帮助?

from tkinter import *
from tkinter.ttk import *
from PIL import Image, ImageTk
from tkinter.filedialog import asksaveasfile
import tkinter.messagebox as messagebox

import importlib
import os
import sys

class Window:
    def _int_(self, master):
        self.master=master
        self.frame=tk.Frame(self.master)
        self.frame.pack()

#web window
window = Tk()

window.title("NT3 Application")

window.geometry('880x550')

module_path = os.path.abspath(os.getcwd() + '\\..')
if module_path not in sys.path:
    sys.path.append(module_path)
    
    
space_label0 = Label(window, text= "", font='Helvetica 11 bold')
space_label0.grid(column=0, row=0)

space_label1 = Label(window, text= "", font='Helvetica 11 bold')
space_label1.grid(column=0, row=1)

space_label2= Label(window, text= "", font='Helvetica 11 bold')
space_label2.grid(column=0, row=3)

space_label3= Label(window, text= "", font='Helvetica 11 bold')
space_label3.grid(column=0, row=4)

space_label4= Label(window, text= "", font='Helvetica 11 bold')
space_label4.grid(column=0, row=8)

#Results_textbox
results_label= Label(window, text= "Result", font='Helvetica 11 bold')
results_label.grid(column=3, row=6)

result_text= Text(window, height=20, width=40 )
result_text.grid(column=3, row=7)

#Name_textbox
name_label = Label(window, text= "        Name ", font='Helvetica 11 bold')
name_label.grid(column=2, row=3)

name_textbox = Text(window, height=1, width=20 )
name_textbox.grid(column=3, row=3)


#Status_dropbox
status_label = Label(window, text= "Status ", font='Helvetica 11 bold')
status_label.grid(column=0, row=3)

combo = Combobox(window, text = "Model")
combo['values']= ("Select","Epochs","Time")

combo.current(0) 
combo.grid(column=1, row=3)

#Module_run

def run_model_text():
    results= %run 'nt3_baseline_keras2.py'
    results.insert(result_text.get)

        
#Run_button
run_btn = Button(window, text="Run",command=run_model_text)
run_btn.grid(column=4, row=3)

#Savebutton
def save_file():
    myFile = filedialog.asksaveasfile(mode='w', defaultextension='.txt')
    if myFile is None:
       return
    data = result_text.get(1.0,'end')
    myFile.write(data)
    myFile.close 

save = Button(window, text="Save", command=save_file)
save.grid(column=3, row=8)    
    
#Plot
plot_label= Label(window, text= "Plot", font='Helvetica 11 bold')
plot_label.grid(column=1, row=6)


tempplot_textbox = Text(window, height=20, width=40 )
tempplot_textbox.grid(column=1, row=7)


window.mainloop()

This is the code I have so far, it may not run for you because of the file I am trying to run.这是我到目前为止的代码,由于我试图运行的文件,它可能无法为您运行。 The problem is that it will run but it will not display on the GUI text area which I have labeled as result_text in the code.问题是它会运行,但不会显示在我在代码中标记为 result_text 的 GUI 文本区域上。

I am no master in tkinter but shouldn't you pack the widgets in the end so that they are placed in GUI window?我不是 tkinter 的大师,但你不应该最后打包小部件以便将它们放在 GUI window 中吗?

ex: result_text.pack() (right before starting the gui at window.mainloop() )例如:result_text.pack()(在 window.mainloop() 开始 gui 之前)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM