簡體   English   中英

有沒有辦法將 Python Shell 輸出放在 tkinter 窗口中?

[英]Is there a way of putting the Python Shell output in a tkinter window?

我想知道是否有可能(如果有,如何)在我制作的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窗口中插入了一個終端模擬器。 我修改了程序,在 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

這是終端工作的屏幕截圖,我在同一個窗口中打包了一個按鈕,只是為了顯示終端真的在框架中:

在此處輸入圖片說明

您總是可以嘗試評估(字符串),捕獲輸出並將其打印到某個文本框。

python 附帶的IDLE 編輯器實際上是通過將 python shell 嵌入到 Text tkinter 小部件中來做到這一點的。

如果您找到或下載 idlelib 源:

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

您應該能夠運行編輯器並檢查/修改代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM