Seite 1 von 1

Buch-Programm stürzt ab - warum?

Verfasst: Freitag 23. Februar 2018, 20:22
von Strawk
Hallo!

Warum stürzt folgendes Programm unter Python 2.7 ab? Erst "keine Rückmeldung", dann "Programm schließen".

Code: Alles auswählen

#!/usr/bin/env python
# tkhello.py -- Hello, World as a Tkinter application

import sys
from Tkinter import *

def build_gui():
    "Build the GUI. Return root, entry, and personalized greeting label"

    rootWindow = Tk()
    rootWindow.wm_geometry("500x200")
    
    label1 = Label(rootWindow)
    label1['text'] = "Hello, Tkinter World!"
    label1.pack()
    
    label2 = Label(rootWindow)
    label2['text'] = "What's your name?"
    label2.pack()

    nameEntry = Entry(rootWindow)
    nameEntry.bind('<Key-Return>', entry_callback)
    nameEntry.pack(fill=X)

    okButton = Button(rootWindow)
    okButton['text'] = 'OK'
    okButton['command'] = entry_callback
    okButton.pack(fill=X)
    
    exitButton = Button(rootWindow)
    exitButton['text'] = 'Exit'
    exitButton['command'] = exit_callback
    exitButton.pack(fill=X)

    outLabel = Label(rootWindow)
    outLabel['text'] = ''
    outLabel.pack()
    
    return rootWindow, nameEntry, outLabel

def entry_callback(event=None):
    "Called when the Return key is hit in the entry field or OK is clicked"
    name = theEntry.get()
    theLabel['text'] = "Nice to meet you, %s" % name

def exit_callback():
    "Called when the Exit button is hit"
    sys.exit(0)

def main():
    global theRoot, theEntry, theLabel
    theRoot, theEntry, theLabel = build_gui()
    theRoot.mainloop()

if __name__ == '__main__':
    main()
Grüße
Strawk

Re: Buch-Programm stürzt ab - warum?

Verfasst: Freitag 23. Februar 2018, 22:06
von noisefloor
Hallo,

kann ich nicht nachvollziehen. Läuft hier wie gewünscht. Was die Struktur des Codes nicht besser macht - aber läuft.

System: Ubuntu 16.04, Python 2.7.12 aus den Paketquellen, Paket python-tk aus den Paketquellen-

Gruß, noisefloor

Re: Buch-Programm stürzt ab - warum?

Verfasst: Samstag 24. Februar 2018, 16:07
von funkheld
Hallo, ich kann keinen Fehler finden.

Ich habe mit Win10 Python 2.7.14 auf dem Notebook.

Es läuft wenn ich es mit der python.exe starte und läuft auch als Compilat von der py2exe.
Ich kann den Text eingeben und der wird unten mit der Begrüßung ausgedruckt.

Gruss

Re: Buch-Programm stürzt ab - warum?

Verfasst: Samstag 24. Februar 2018, 17:09
von Strawk
Hallo!

Und das Programm stürzt auch dann nicht ab?:
  • Anaconda Navigator Spyder
  • Betätigung des Exit-Buttons
Grüße
Strawk

Re: Buch-Programm stürzt ab - warum?

Verfasst: Samstag 24. Februar 2018, 17:16
von noisefloor
Hallo,

exit-Button funktioniert wie er soll. Anaconda / Spyder installiere ich mir jetzt dafür aber nicht extra auf meinem Linux-System.

Gruß, noisefloor

Re: Buch-Programm stürzt ab - warum?

Verfasst: Montag 26. Februar 2018, 08:07
von kaytec
Hallo Strawk,
läuft auf Ubuntu 16.04.3 LTS/Linux 4.4.0-116-generic (x86_64)/Desktop XFCE4.

Gruß Frank