Seite 1 von 1

tkinter + base64 - problem

Verfasst: Mittwoch 10. August 2011, 12:41
von jtschoch
Hi Leute,

ich habe ein base64 Converter programmiert aber,
mit dem base64 funktioniert das bei Python 3.x nicht.

Könnt ihr mir bitte helfen?

Mein Programm:

Code: Alles auswählen

# -*- coding: UTF-8 -*-
from tkinter import ttk
from tkinter import*
import tkinter
import base64
import tkinter.filedialog

style = ttk.Style()
style.theme_use('xpnative')
root.attributes('-toolwindow', True)                              
root.title("base64 Converter")
root.geometry('560x200')

def ask_quit():
    root.destroy()
    root.mainloop()
    
def pfadopen():

    pfad = tkinter.filedialog.askopenfilename(filetypes=[("Alle Images", ".jpg .jpeg .gif .png"),("JPG Files", ".jpg"),("JPEG Files", ".jpeg")])
    print (pfad)
    f = open(pfad, 'rb')
    data = f.read()
    ausgabe=data.encode('base64')
    f.close()
    ausgabe = f
    etext.insert("end",ausgabe)


scrollbar = ttk.Scrollbar(root)
scrollbar.place(relx=0.958, rely=0.04, relheight=0.55)
text = Text(root,yscrollcommand=scrollbar.set)
scrollbar.config(command=text.yview)
text.config(font =("Arial", 11))  
text.insert(INSERT, r"")
text.place(relx=0.01, rely=0.04, relwidth=0.95, relheight=0.55)


pfad1 = ttk.Button(root, text = "Pfad öffnen", command = pfadopen)
pfad1.place(relx=0.01, rely=0.7, relwidth=0.2, relheight=0.2)

exitwindow = ttk.Button(root, text = "Beenden", command = ask_quit)
exitwindow.place(relx=0.3, rely=0.7, relwidth=0.2, relheight=0.2)

root.mainloop()

Re: tkinter + base64 - problem

Verfasst: Mittwoch 10. August 2011, 19:33
von jtschoch
Kann mir niemand helfen? :( :( :( :!: :idea: :K

Re: tkinter + base64 - problem

Verfasst: Mittwoch 10. August 2011, 20:13
von wuf
Hi jtschoch

Wir versuchen doch immer zu helfen. Kannst du das folgende einmal ausprobieren:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: UTF-8 -*-


from tkinter import*
from tkinter import ttk
import tkinter
import base64
import tkinter.filedialog

root = Tk()

style = ttk.Style()
#style.theme_use('xpnative')
#root.attributes('-toolwindow', True)                              
root.title("base64 Converter")
root.geometry('600x400')


def ask_quit():
    root.destroy()
    root.mainloop()
   
def pfadopen():

    pfad = tkinter.filedialog.askopenfilename(filetypes=[("Alle Images",
        ".jpg .jpeg .gif .png"),("JPG Files", ".jpg"),("JPEG Files", ".jpeg")])
    print (pfad)
    
    with open(pfad, 'rb') as f:
        data = f.read()
        ausgabe = base64.encodebytes(data)
        
    text.insert("end",ausgabe)
    image_label.imported_image = PhotoImage(data=ausgabe)
    image_label.config(image=image_label.imported_image) 
    
scrollbar = ttk.Scrollbar(root)
scrollbar.place(relx=0.958, rely=0.04, relheight=0.55)

text = Text(root, yscrollcommand=scrollbar.set, bg='white', fg='darkolivegreen',
    padx=5, pady=5, font=("Monospace", -12, "normal"))
    
scrollbar.config(command=text.yview)

text.insert(INSERT, r"")
text.place(relx=0.01, rely=0.04, relwidth=0.95, relheight=0.55)

pfad1 = ttk.Button(root, text = "Pfad öffnen", command = pfadopen)
pfad1.place(relx=0.01, rely=0.7, relwidth=0.2, relheight=0.2)

exitwindow = ttk.Button(root, text = "Beenden", command = ask_quit)
exitwindow.place(relx=0.3, rely=0.7, relwidth=0.2, relheight=0.2)

image_label = ttk.Label(root, relief='sunken', anchor='center')
image_label.place(relx=0.6, rely=0.7, relwidth=0.2, relheight=0.2)
image_label.imported_image = None

root.mainloop()
Ist es dir noch nicht verleidet mit dem Place-Layout-Manger zu arbeiten?

Gruß wuf :wink:

Re: tkinter + base64 - problem

Verfasst: Donnerstag 11. August 2011, 11:21
von jtschoch
Vielen Dank!
SO wollte ich das haben, ich hatte mit base64 alles versucht aber nichts hat geglappt.
Aber mit der Vorschau glappt nicht richtig, weil es nicht alle Formate unterstützt!
----------------
Achso mit dem
Ist es dir noch nicht verleidet mit dem Place-Layout-Manger zu arbeiten?
Ich habe es mir so schon angewönt mit pack und gird komme ich nicht so richtig klar.
Hier kann ich besser die Position bestimmen.

Re: tkinter + base64 - problem

Verfasst: Donnerstag 11. August 2011, 12:55
von BlackJack
@jtschoch: Solange Du keine Programme schreibst die jemand anderes benutzen möchte — aber spätestens dann ist `place()` einfach nicht mehr praktikabel. Eben weil Du die Positionen bestimmst, funktioniert das nur solange alle Rahmenbedingungen wie Bildschirmauflösung, Schriftgrössen, Fenstersystem, und so weiter, denen *Deines* Rechners entsprechen.

Re: tkinter + base64 - problem

Verfasst: Donnerstag 11. August 2011, 15:35
von jtschoch
Bild

So sieht es jetzt aus!

Aber fält euch was auf? Es wird nur ein Teil des Bildes angezeigt.
Wie kann ich das ändern das die Bilder vollständig in den Kasten angezeigt werden?

----------------
Achso blos einwas stört mich bei Place() das Die Buttons so groß werden!

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from tkinter import*
from tkinter import ttk
import tkinter
import base64
import tkinter.filedialog

root = Tk()

style = ttk.Style()
#style.theme_use('xpnative')
#root.attributes('-toolwindow', True)                              
root.title("base64 Converter")
#root.geometry('600x400')
root.geometry('560x200')

def ask_quit():
    root.destroy()
    root.mainloop()
   
def pfadopen():

    pfad = tkinter.filedialog.askopenfilename(filetypes=[("Alle Images",
                                                          ".jpg .jpeg .bmp .ico .gif .raw .png"),
                                                         ("JPG JPEG File Interchange Format", ".jpg .jpeg"),
                                                         ("Windows Bitmap", ".bmp"),
                                                         ("Gimp\Windows Icon", ".ico"),
                                                         ("Portable Network Graphics", ".png"),
                                                         ("RAW Graphics Format/ Rohdatenformat", ".raw"),
                                                         ("Graphics Interchange Format", ".gif"),
                                                         ("other", "*.*")])

    try:
        
        with open(pfad, 'rb') as f:
            data = f.read()
            ausgabe = base64.encodebytes(data)
    except:
        ausgabe = " ***Kein Bild ausgewählt***"
    try:    
        text.insert("end",ausgabe)
        image_label.imported_image = PhotoImage(data=ausgabe)
        image_label.config(image=image_label.imported_image) 
    except:
        text.insert("end",ausgabe)
        image_label.config(text = " Keine Vorschau\n verfügbar",
                           font=("Monospace", -16, "bold")) 

scrollbar = ttk.Scrollbar(root)
scrollbar.place(relx=0.578, rely=0.04, relheight=0.55)

text = Text(root, yscrollcommand=scrollbar.set, bg='white', fg='darkolivegreen',
    padx=5, pady=5, font=("Monospace", -12, "normal"))
    
scrollbar.config(command=text.yview)

text.insert(INSERT, r"")
text.place(relx=0.01, rely=0.04, relwidth=0.57, relheight=0.55)

pfad1 = ttk.Button(root, text = "Pfad öffnen", command = pfadopen)
pfad1.place(relx=0.01, rely=0.7, relwidth=0.2, relheight=0.2)

exitwindow = ttk.Button(root, text = "Beenden", command = ask_quit)
exitwindow.place(relx=0.3, rely=0.7, relwidth=0.2, relheight=0.2)

image_label = ttk.Label(root, relief='sunken', anchor='center')
image_label.place(relx=0.68, rely=0.04, relwidth=0.3, relheight=0.9)
image_label.imported_image = None

ttk.Sizegrip(root).place(relx=0.969, rely=0.92)

    
root.mainloop()

Re: tkinter + base64 - problem

Verfasst: Donnerstag 11. August 2011, 15:52
von Xynon1
Sag mal hat das Forum irgendwie einen sehr wichtigen Platz in deinem Kopf übernommen? Entweder du skalierst das Bild oder du machst dein Anzeigefenster größer. Es ist doch dein Programm, dann solltest du auch Wissen wie es letztendlich aussehen soll; z.B. deine Buttons, wenn diese ihre Größe nicht ändern sollen dann lass doch die relativen Größenangaben weg. Weißt du überhaupt auch nur ansatzweise was du hier manchmal für Fragen stellst? (und dann auch noch die Antworten ignorierst, solange nicht die vollständige Lösung drin steht)

Re: tkinter + base64 - problem

Verfasst: Donnerstag 11. August 2011, 17:20
von jtschoch
Ich möchte es ja skalieren, aber ich weiß nicht wie das geht.
Ich habe auch schon unter den Suchbegriff: python photoImage skalieren geschaut, aber nicht das Richtige!

Re: tkinter + base64 - problem

Verfasst: Donnerstag 11. August 2011, 17:30
von Xynon1
Und nochmal: Schau dir endlich die PIL an.

Re: tkinter + base64 - problem

Verfasst: Donnerstag 11. August 2011, 17:46
von jtschoch
ja, aber mein Python 27 ist halb kaputt besser gesagt es ist nichtmer da!
Habe ich mir schon angeschaut

Re: tkinter + base64 - problem

Verfasst: Donnerstag 11. August 2011, 20:41
von Newcomer
das passt jetzt zwar nicht zum Thema, aber ich find python 3.2 am besten ;DDDDDDD

Edit: Wow du schreibst ca. zwei beiträge am tag

Re: tkinter + base64 - problem

Verfasst: Freitag 12. August 2011, 05:25
von EyDu
jtschoch hat geschrieben:ja, aber mein Python 27 ist halb kaputt besser gesagt es ist nichtmer da!
Das ist ja nicht unser Problem ;-) Vielleicht solltst du das erstmal reparieren.

Re: tkinter + base64 - problem

Verfasst: Freitag 12. August 2011, 22:07
von jtschoch
Habe ich schon getan
-----------------

Ich möchte das Bild skalieren, also sozusagen ein thumbnail.
Habe nur bei Pil habe ich was gefunden:
http://www.pythonware.com/library/pil/h ... /image.htm

Re: tkinter + base64 - problem

Verfasst: Freitag 12. August 2011, 22:30
von jtschoch
ja aber jetzt kommt das Problem:

Ich muss mein Prog. in 2.7 schreiben und da weiß ich das wieder nicht wie das mit den base64 ist und Pil hat auch ein Prob.

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from Tkinter import* 
import Tkinter, ttk
import base64
import tkFileDialog
from PIL import Image
import os

root = Tk()

style = ttk.Style()
#style.theme_use('xpnative')
#root.attributes('-toolwindow', True)                              
root.title("base64 Converter")
#root.geometry('600x400')
root.geometry('560x200')

def ask_quit():
    root.destroy()
    root.mainloop()
   
def pfadopen():

    pfad = tkFileDialog.askopenfilename(filetypes=[("Alle Images",
                                                    ".jpg .jpeg .bmp .ico .gif .raw .png"),
                                                    ("JPG JPEG File Interchange Format", ".jpg .jpeg"),
                                                    ("Windows Bitmap", ".bmp"),
                                                    ("Gimp\Windows Icon", ".ico"),
                                                    ("Portable Network Graphics", ".png"),
                                                    ("RAW Graphics Format/ Rohdatenformat", ".raw"),
                                                    ("Graphics Interchange Format", ".gif"),
                                                    ("other", "*.*")])

    try:
        
        with open(pfad, 'rb') as f:
            data = f.read()
            #ausgabe = base64.encode(data)
            ausgabe = data
    except:
        ausgabe = " ***Kein Bild ausgewählt***"
    #try:
    size = 128, 128
    text.insert("end",ausgabe)
    image_label.imported_image = Image.open(ausgabe)
    image_label.imported_image.thumbnail(size, Image.ANTIALIAS)
    image_label.config(image=image_label.imported_image) 
    #except:
        #text.insert("end",ausgabe)
        #image_label.config(text = " Keine Vorschau\n verfügbar",
                           #font=("Monospace", -16, "bold")) 

scrollbar = ttk.Scrollbar(root)
scrollbar.place(relx=0.578, rely=0.04, relheight=0.55)

text = Text(root, yscrollcommand=scrollbar.set, bg='white', fg='darkolivegreen',
    padx=5, pady=5, font=("Monospace", -12, "normal"))
    
scrollbar.config(command=text.yview)

text.insert(INSERT, r"")
text.place(relx=0.01, rely=0.04, relwidth=0.57, relheight=0.55)

pfad1 = ttk.Button(root, text = "Pfad öffnen", command = pfadopen)
pfad1.place(relx=0.01, rely=0.7, relwidth=0.2, relheight=0.2)

exitwindow = ttk.Button(root, text = "Beenden", command = ask_quit)
exitwindow.place(relx=0.3, rely=0.7, relwidth=0.2, relheight=0.2)

image_label = ttk.Label(root, relief='sunken', anchor='center')
image_label.place(relx=0.68, rely=0.04, relwidth=0.3, relheight=0.9)
image_label.imported_image = None

ttk.Sizegrip(root).place(relx=0.969, rely=0.92)

    
root.mainloop()
Und noch was:
wie kann ich das machen das das Prog. immer den alten text überschreibt und nicht hinten dran hängt.

Re: tkinter + base64 - problem

Verfasst: Sonntag 14. August 2011, 10:17
von jtschoch
Kann mir bitte jemand helfen :?: :roll: :( :( :( :( :( :( :arrow: no :idea:

Re: tkinter + base64 - problem

Verfasst: Mittwoch 31. August 2011, 17:37
von jtschoch
Ja, ich habe das hinbekommen, aber
ich wollte das Bild mit thumbnail kleiner machen aber da bekomme ich eine Fehlermeldung:

Code: Alles auswählen

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=128x91 at 0x1697418>

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "J:\JT_proj\python\base64\mybase642.py", line 50, in pfadopen
    image_label.config(image=image_label.imported_image)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1202, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1193, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: image specification must contain an odd number of elements
Meine code Hier