【问题标题】:Is there a way of putting the Python Shell output in a tkinter window?有没有办法将 Python Shell 输出放在 tkinter 窗口中?
【发布时间】:2015-05-13 18:01:22
【问题描述】:

我想知道是否有可能(如果有,如何)在我制作的tkinter 窗口中输出和输入Python Shell。我在谷歌上搜索,但我似乎找不到任何东西。如果可能,是否有初学者可以理解的简短版本。 (我尝试过的所有网站我都无法理解。)

这是我的代码:

from tkinter import *



def Exit():
    print()

def newClassResults():
    #assigns variable to an input so it can be reffered to later
    amount = int(input("How many people would you like to add? "))

    #starts for loop
    for counter in range(amount):
#assigns inputto a  variable called 'newName'
        newName = input("\nEnter the new student's name: ")
#assigns input to a  variable called 'newScore'
        newScore = int(input("Enter the new student's score: "))
#adds new results to the list below
        students_and_score.append((newName,newScore))
        score.append(newScore)

def saveResults():
#imports time module so that the program can pause for a certain amount of time
    import time
    print("\nSaving...")
    import random, decimal
    time1 = decimal.Decimal(random.randrange(1,10))/10
    time.sleep(time1)
    print("\nStudents' names saved")
    print("Students' scores saved")

def sortResults():
#imports operator module 
    import operator
#imports time module 
    import time
    #sorts results in acsending order
    students_and_score.sort(key=operator.itemgetter(1))
#prints in ascending order
    print("\nSorting Results...")
    import random, decimal
    time1 = decimal.Decimal(random.randrange(1,10))/10
    time.sleep(time1)
    print(students_and_score)

def percentageCalc():
#assigns input to variable called 'number'
    number = int(input("\nEnter minimum mark: "))
#creates variable called 'size'
    size = len(score)
    index = 0
    for counter in range(size):
        if score[index] > number:
            higher.append(score[index])
        index = index + 1
    higherLength = len(higher)
#calculates percentage of people with score over amount entered
    finished = higherLength / size
    finished = finished * 100
#rounds percentage
    finished = round(finished)
#creates space between line
    print("\n")
    print(finished,"% of your students got over",number,"marks")

def printResults():
#starts for loop
    for pair in students_and_score:
#creates space between line
        print("\n")
#changes layout of list so it is more readable
        print(pair[0],pair[1])

#assigns list to a variable
students_and_score = [("Imelda Thomas",74),("Craig Parr",90),("Eric     Salisbury",58),("Laurence Mann",35),("Bill Walford",82),("David Haroald",27),("Pamela Langley",43),("Sarah Boat",39),("Rachel Matthews",62),("Michaela Cunningham",69)]
score = [74,90,58,35,82,27,43,39,62,69]
higher = []

window = Tk()
#sets background colour
window.configure(background="white")
#assigns title
window.title("Menu")
#sets the size of window
window.geometry("300x300")
window.wm_iconbitmap('favicon.ico')

menu = Menu(window)


subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="Exit", command=Exit)

subMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=subMenu)
subMenu.add_command(label="Enter New Class Results",     command=newClassResults)
subMenu.add_separator()
subMenu.add_command(label="Save Results", command=saveResults)
subMenu.add_command(label="Sort Results", command=sortResults)
subMenu.add_command(label="Print Results", command=printResults)
subMenu.add_separator()
subMenu.add_command(label="Calculate Percentage", command=percentageCalc)

#Finishes off
window.config(menu=menu)
window.mainloop()

【问题讨论】:

  • 要显示输出,您可以使用 tkinter Text 小部件并获取输入,您可以使用 Entry 小部件(最终使用 Button,即使您不严格需要它).. 可能发生的一个问题是,如果您在后台进行大量计算,您的 GUI 可能会冻结,在这种情况下,您可以使用线程或 after 函数...
  • 那么如何将 Text 小部件应用到我的代码中?
  • 这能回答你的问题吗? build cmd into Tkinter window

标签: python shell tkinter


【解决方案1】:

看看this post。

作者在tkinter 窗口中插入了一个终端仿真器。我已经修改了程序,在 tkinter 窗口中插入了启动 python 的命令:

#!/usr/bin/python

from Tkinter import *
import os

root = Tk()
termf = Frame(root, width = 400, height = 200)

termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
os.system('xterm -into %d -geometry 80x20 -sb -e python &' % wid)

root.mainloop()

这可能不适用于 Windows,因为没有 xterm。

这是终端工作的截图,我在同一个窗口中打包了一个按钮,只是为了显示终端真的在框架中:

【讨论】:

  • 我仍然不相信这段代码是否能在 Mac OS X 上完成它应该做的事情,因为发生在我身上的是 Python shell 部分是在 xterm 上启动的,没有别的(只有当我指定到xterm 的整个路径时,我才看到你想用这段代码实现什么......这段代码应该做什么?对我来说,模拟器没有插入框架中,但它独立启动并且同时运行一个空的 tkinter 窗口..
  • 它可能不适用于 Max OS(我无法测试),但它适用于 Linux。发帖人没有提及(或标记)他是否使用 Mac OS。我已经添加了结果的屏幕截图。
  • 注意:对于 python3 你需要 >apt install python3-tk 并且导入是:“from tkinter import *”
【解决方案2】:

您可以随时尝试eval(string),捕获输出并将其打印到某个文本框。

【讨论】:

    【解决方案3】:

    python 附带的IDLE editor 实际上正是通过将 python shell 嵌入到 Text tkinter 小部件中来实现这一点的。

    如果您找到或下载了 idlelib 源代码:

    https://github.com/python/cpython/tree/master/Lib/idlelib

    您应该能够只运行编辑器并检查/修改代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      • 2021-04-28
      • 2013-08-26
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多