2015-05-13 6 views
5

Mi chiedevo se è possibile (e se sì, come) avere l'output Python Shell e l'input all'interno di una finestra tkinter che ho creato. Ho cercato su google ma non riesco a trovare nulla. Se possibile, c'è una versione breve che un principiante potrebbe capire. (Tutti i siti web che ho provato io proprio non riuscivano a capire.)C'è un modo per inserire l'output di Python Shell in una finestra tkinter?

Ecco il mio codice che ho ottenuto:

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() 
+0

Per mostrare l'output, è possibile utilizzare un widget 'Text' di tkinter e per ottenere l'input è possibile utilizzare un widget' Entry' (ed eventualmente un 'Button', anche se non ne è strettamente necessario) .. Un problema che può accadere è che, se si hanno molte compresenze dietro le quinte, la GUI potrebbe bloccarsi, in tal caso si potrebbero usare discussioni o forse la funzione 'dopo' ... – nbro

+0

Quindi, come applicherò il widget Testo al mio codice? – AlexDear

risposta

5

Dai un'occhiata alla this post.

L'autore inserisce un emulatore di terminale in una finestra tkinter. Ho modificato il programma, inserendo il comando per avviare python nella finestra Tkinter:

#!/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() 

Questo potrebbe non funzionare in Windows anche se, come non v'è alcun xterm.

Questo è uno screenshot della lavorazione del terminale, e ho confezionato un pulsante nella stessa finestra, solo per mostrare il terminale è in realtà nella cornice:

enter image description here

+0

Non sono ancora convinto se questo codice fa quello che dovrebbe fare su un Mac OS X, perché quello che succede a me è che una sezione di shell Python viene avviata su 'xterm' e nient'altro (e solo se specifichi l'intero percorso per 'xterm'), non vedo cosa si vuole ottenere con questo codice ... cosa deve fare questo codice? Per me l'emulatore non è inserito nel Frame, ma si avvia in modo indipendente e una finestra tkinter vuota viene eseguita allo stesso tempo. – nbro

+0

Potrebbe non funzionare su Max OS (non posso testarlo), ma funziona su Linux. Il poster non menzionava (o taggava) se stava usando Mac OS. Ho aggiunto uno screenshot del risultato. – jcoppens

0

Si può sempre cercare di eval (stringa), acquisisce l'output e lo stampa in una casella di testo.

Problemi correlati