/ / Come creare un'interfaccia utente in Python [closed] - python, interfaccia utente, raw-input

Come creare un'interfaccia utente in Python [closed] - python, interfaccia utente, raw-input

Quindi ho il codice ...

name = raw_input("Full Name: ")

E questo dovrebbe permettermi di digitare anome e quindi utilizzare quel nome per attivare alcune affermazioni "se". Tuttavia, voglio che il programma apra una finestra, come una GUI molto semplice, in cui posso digitare la parola. La mia unica esperienza con la creazione di una GUI è in Java e attraverso Processing, quindi sono molto perso quando si tratta di Python. Qualsiasi aiuto è apprezzato.

risposte:

1 per risposta № 1

Per iniziare ti suggerisco di iniziare con la libreria Tkinter (costruita nella libreria).

Questo è un semplice programma che usa Tkinter gui.

import Tkinter

class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()

def initialize(self):
self.grid()

self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
self.entry.grid(column=0,row=0,sticky="EW")
self.entry.bind("<Return>", self.OnPressEnter)
self.entryVariable.set(u"Enter text here.")

button = Tkinter.Button(self,text=u"Click me !",
command=self.OnButtonClick)
button.grid(column=1,row=0)

self.labelVariable = Tkinter.StringVar()
label = Tkinter.Label(self,textvariable=self.labelVariable,
anchor="w",fg="white",bg="blue")
label.grid(column=0,row=1,columnspan=2,sticky="EW")
self.labelVariable.set(u"Hello !")

self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)

def OnButtonClick(self):
self.labelVariable.set( self.entryVariable.get()+" (You clicked the button)" )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)

def OnPressEnter(self,event):
self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)

if __name__ == "__main__":
app = simpleapp_tk(None)
app.title("my application")
app.mainloop()

Il codice preso da http://sebsauvage.net/python/gui/ , mi piace il loro tutorial.


-1 per risposta № 2

Tkinter è un ottimo modo per creare una GUI veloce madovrebbe essere chiarito che Tkinter stesso non fa parte di Python. Il modulo Tkinter in Python è l'interfaccia incorporata al toolkit TKinter GUI che viene installato separatamente. Inoltre, qualsiasi computer su cui si desidera eseguire questo programma dovrà avere installato Tkinter in aggiunta a Python.

EDIT: Questo è vero se stai sviluppando su Linux.Se stai usando Windows o Mac, tcl / tk è in bundle nel programma di installazione di Python quindi è la soluzione Python automatica.

TL; DR: Tkinter è un ottimo modo per iniziare ma non è la soluzione Python automatica.

Vedere: https://docs.python.org/2/library/tkinter.html per iniziare con Tk