Texteingabe übersetzen

Fragen zu Tkinter.
Antworten
Benutzeravatar
michi-monster
User
Beiträge: 48
Registriert: Donnerstag 7. Dezember 2017, 18:30
Kontaktdaten:

Halli, ich bin dabei ein Programm zu entwickeln, dass Häkelanleitungen übersetzt. Dafür habe ich ein dictonary erstellt. Leider bekomme ich es nicht hin ein grafisches Feld zu erstellen wo der User den Text eingibt und der dann übersetzt wird. Bitte helft mir mal.

Code: Alles auswählen

from tkinter import *
import re,sys,tkinter
def ende():
  sys.exit(0)
def eingabe():
        eingabe = eingabefeld.get()
fenster=tkinter.Tk()  
class App:
  def __init__(self, master):
    frame = Frame(master)
    frame.pack()
    self.button = Button(frame, 
                         text="EXIT", fg="red",
                         command=ende)
    self.button.pack(side=LEFT)
    self.slogan = Button(frame,
                         text="Übersetzung starten",
                         command=self.write_slogan)
    self.slogan.pack(side=LEFT)
  def write_slogan(self):
    print ("Übersetzung")

#root = Tk()
#app = App(root)
#root.mainloop()

text = str(input('Text eingeben : '))




translation_table = {'repeat 6 times':'6x wiederholen',
                     'sc':'fM',
                     'magic ring':'Fadenring',
                     'inc':'zun',
                     'in next st':'in die nächsten Maschen',
                     'in next 2 st':'in 2 Maschen',
                     'in next 8 st':'in die nächsten 8 Maschen',
                     'dec':'abn',
                    '6 sc in magic ring':'6 fM in den Fadenring',
                     'in the next':'in die nächsten',
                     'sl st' :'KM',
                     'in all' : 'in Alle',
                     'st':'Masche',
                     'in next':'in die nächsten',
                     'repeat 3 times':'3x wiederholen',
                     'repeat 4 times' : '4x wiederholen',
                     'repeat 2 times' : '2x wiederholen',
                     'fM in all 18 st':'fM in alle 61 Maschen',
                     '1inc':'1 Zun',
                     '2inc':'2 Zum',
                     '3inc':'3 Zun.',
                     '4inc':'4 Zun.',
                     'Round':'Runde',
                     '1sc':'1 fM',
                     '2sc':'2 fM',
                     '3sc':'3 fM',
                     '4sc':'4 fM',
                     '5sc':'5 fM',
                     '6sc':'6 fM',
                     '7sc':'7 fM',
                     'Repeat':'wiederholen',
                     'MR':'Fadenring',
                     'times':'mal',
                     'single crochet':'feste Masche',
                     'Legs':'Füße',
                     'Row 1':'Reihe 1 ',
                     'Row 2':'Reihe 2 ',
                     'Row 3':'Reihe 3 ',
                     'Row 4':'Reihe 4 ',
                     'Row 5':'Reihe 5 ',
                     'Row 6':'Reihe 6 ',
                     'Row 7':'Reihe 7 ',
                     'Row 8':'Reihe 8 ',
                     'Row 9':'Reihe 9 ',
                     'Row 10':'Reihe 10 ',
                     'Row 11':'Reihe 11 ',
                     'Row 12':'Reihe 12 ',
                     'Row 13':'Reihe 13 ',
                     'Row 14':'Reihe 14 ',
                     'Row 15':'Reihe 15 ',
                     'Row 16':'Reihe 16 ',
                     'Row 17':'Reihe 17 ',
                     'Row 18':'Reihe 18 ',
                     'Row 19':'Reihe 19 ',
                     'Row 20':'Reihe 20 ',
                     'Row1':'Reihe 1 ',
                     'Row2':'Reihe 2 ',
                     'Row3':'Reihe 3 ',
                     'Row4':'Reihe 4 ',
                     'Row5':'Reihe 5 ',
                     'Row6':'Reihe 6 ',
                     'Row7':'Reihe 7 ',
                     'Row8':'Reihe 8 ',
                     'Row9':'Reihe 9 ',
                     'Row10':'Reihe 10 ',
                     'Row11':'Reihe 11 ',
                     'Row12':'Reihe 12 ',
                     'Row13':'Reihe 13 ',
                     'Row14':'Reihe 14 ',
                     'Row15':'Reihe 15 ',
                     'Row16':'Reihe 16 ',
                     'Row17':'Reihe 17 ',
                     'Row18':'Reihe 18 ',
                     'Row19':'Reihe 19 ',
                     'Row20':'Reihe 20 ',
                     'Row 1':'Reihe 1 ',
                     'Row 1:':'Reihe 1 ',
                      'Row2:':'Reihe 2 ',
                     'Row3:':'Reihe 3 ',
                     'Row4:':'Reihe 4 ',
                     'Row5:':'Reihe 5 ',
                     'Row6:':'Reihe 6 ',
                     'Row7:':'Reihe 7 ',
                     'Row8:':'Reihe 8 ',
                     'Row9:':'Reihe 9 ',
                     'Row10:':'Reihe 10 ',
                     'Row11:':'Reihe 11 ',
                     'Row12:':'Reihe 12 ',
                     'Row13:':'Reihe 13 ',
                     'Row14:':'Reihe 14 ',
                     'Row15:':'Reihe 15 ',
                     'Row16:':'Reihe 16 ',
                     'Row17:':'Reihe 17 ',
                     'Row18:':'Reihe 18 ',
                     'Row19:':'Reihe 19 ',
                     'Row20:':'Reihe 20 ',
                     'Stuff the arms from time to time, not very tightly':'Fülle die Arme von Zeit zu Zeit, nicht sehr fest',
                     'Fix and leave the thread to sew the arms to the body.':'Abbinden und einen Faden zum Annähen lassen.',

}

pattern = re.compile(r'\b(' + '|'.join(translation_table.keys()) +r')\b')
result = pattern.sub(lambda x : translation_table[x.group()], text)
print("Der folgende Text wurde übersetzt: ",result)
Zuletzt geändert von __deets__ am Montag 21. Mai 2018, 17:31, insgesamt 1-mal geändert.
Grund: Code Tags
Benutzeravatar
michi-monster
User
Beiträge: 48
Registriert: Donnerstag 7. Dezember 2017, 18:30
Kontaktdaten:

Nachtrag: die Einrückungen wurden nicht übernommen. Grafisch hab ich es hier: http://edv-bildung.de/python-uebersetzung
Benutzeravatar
ThomasL
User
Beiträge: 1366
Registriert: Montag 14. Mai 2018, 14:44
Wohnort: Kreis Unna NRW

Hi,
habe leider kein Tkinter Wissen, aber der Frederik hat mir in anderen Dingen schon gut mit seinen Anleitungen geholfen.
Hier sein Tkinter Tutorial:
https://www.youtube.com/watch?v=ahtKa4M ... 49RhnvQQVK
Ich bin Pazifist und greife niemanden an, auch nicht mit Worten.
Für alle meine Code Beispiele gilt: "There is always a better way."
https://projecteuler.net/profile/Brotherluii.png
Benutzeravatar
wuf
User
Beiträge: 1529
Registriert: Sonntag 8. Juni 2003, 09:50

Hi michi-moster

Hier etwas zum ausprobieren:

Code: Alles auswählen

import re,sys
import tkinter as tk
        
 
class App(object):
    
    def __init__(self, master, translation_table):
        self.master = master
        self.translation_table = translation_table
      
        frame = tk.Frame(master)
        frame.pack(fill='both', expand=True,padx=10, pady=10)
        
        self.var_eingabe = tk.StringVar(master, "Texteingabe")
        self.eingabe = tk.Entry(frame, textvariable=self.var_eingabe)
        self.eingabe.pack(pady=2)
        
        self.var_ausgabe = tk.StringVar() 
        tk.Label(frame, textvariable=self.var_ausgabe, bg='white', width=20,
            height=10).pack(fill='both', pady=2)
            
        self.slogan = tk.Button(frame, text="Übersetzung starten",
            command=self.write_slogan)
        self.slogan.pack(pady=2)
        
        self.button = tk.Button(frame,text="EXIT", fg="red", command=self.ende)
        self.button.pack(side='top')
        self.eingabe.select_range(0, "end")
        self.eingabe.focus_set()
        
    def ende(self):
        sys.exit(0)
              
    def write_slogan(self):
        print ("Übersetzung")
        eingabe = self.var_eingabe.get()        
        pattern = re.compile(r'\b(' + '|'.join(self.translation_table.keys())
            +r')\b')

        result = pattern.sub(lambda x : self.translation_table[x.group()],
            eingabe)
            
        self.var_ausgabe.set(result)
        self.eingabe.select_range(0, "end")
        self.eingabe.focus_set()
        
def main():
    root = tk.Tk()
    root.title("Übersetzer")

    translation_table = {
        'repeat 6 times':'6x wiederholen',
        'sc':'fM',
        'magic ring':'Fadenring',
        'inc':'zun',
        'in next st':'in die nächsten Maschen',
        'in next 2 st':'in 2 Maschen',
        'in next 8 st':'in die nächsten 8 Maschen',
        'dec':'abn',
        '6 sc in magic ring':'6 fM in den Fadenring',
        'in the next':'in die nächsten',
        'sl st' :'KM',
        'in all' : 'in Alle',
        'st':'Masche',
        'in next':'in die nächsten',
        'repeat 3 times':'3x wiederholen',
        'repeat 4 times' : '4x wiederholen',
        'repeat 2 times' : '2x wiederholen',
        'fM in all 18 st':'fM in alle 61 Maschen',
        '1inc':'1 Zun',
        '2inc':'2 Zum',
        '3inc':'3 Zun.',
        '4inc':'4 Zun.',
        'Round':'Runde',
        '1sc':'1 fM',
        '2sc':'2 fM',
        '3sc':'3 fM',
        '4sc':'4 fM',
        '5sc':'5 fM',
        '6sc':'6 fM',
        '7sc':'7 fM',
        'Repeat':'wiederholen',
        'MR':'Fadenring',
        'times':'mal',
        'single crochet':'feste Masche',
        'Legs':'Füße',
        'Row 1':'Reihe 1 ',
        'Row 2':'Reihe 2 ',
        'Row 3':'Reihe 3 ',
        'Row 4':'Reihe 4 ',
        'Row 5':'Reihe 5 ',
        'Row 6':'Reihe 6 ',
        'Row 7':'Reihe 7 ',
        'Row 8':'Reihe 8 ',
        'Row 9':'Reihe 9 ',
        'Row 10':'Reihe 10 ',
        'Row 11':'Reihe 11 ',
        'Row 12':'Reihe 12 ',
        'Row 13':'Reihe 13 ',
        'Row 14':'Reihe 14 ',
        'Row 15':'Reihe 15 ',
        'Row 16':'Reihe 16 ',
        'Row 17':'Reihe 17 ',
        'Row 18':'Reihe 18 ',
        'Row 19':'Reihe 19 ',
        'Row 20':'Reihe 20 ',
        'Row1':'Reihe 1 ',
        'Row2':'Reihe 2 ',
        'Row3':'Reihe 3 ',
        'Row4':'Reihe 4 ',
        'Row5':'Reihe 5 ',
        'Row6':'Reihe 6 ',
        'Row7':'Reihe 7 ',
        'Row8':'Reihe 8 ',
        'Row9':'Reihe 9 ',
        'Row10':'Reihe 10 ',
        'Row11':'Reihe 11 ',
        'Row12':'Reihe 12 ',
        'Row13':'Reihe 13 ',
        'Row14':'Reihe 14 ',
        'Row15':'Reihe 15 ',
        'Row16':'Reihe 16 ',
        'Row17':'Reihe 17 ',
        'Row18':'Reihe 18 ',
        'Row19':'Reihe 19 ',
        'Row20':'Reihe 20 ',
        'Row 1':'Reihe 1 ',
        'Row 1:':'Reihe 1 ',
        'Row2:':'Reihe 2 ',
        'Row3:':'Reihe 3 ',
        'Row4:':'Reihe 4 ',
        'Row5:':'Reihe 5 ',
        'Row6:':'Reihe 6 ',
        'Row7:':'Reihe 7 ',
        'Row8:':'Reihe 8 ',
        'Row9:':'Reihe 9 ',
        'Row10:':'Reihe 10 ',
        'Row11:':'Reihe 11 ',
        'Row12:':'Reihe 12 ',
        'Row13:':'Reihe 13 ',
        'Row14:':'Reihe 14 ',
        'Row15:':'Reihe 15 ',
        'Row16:':'Reihe 16 ',
        'Row17:':'Reihe 17 ',
        'Row18:':'Reihe 18 ',
        'Row19:':'Reihe 19 ',
        'Row20:':'Reihe 20 ',
        'Stuff the arms from time to time, not very tightly':'Fülle die Arme von Zeit zu Zeit, nicht sehr fest',
        'Fix and leave the thread to sew the arms to the body.':'Abbinden und einen Faden zum Annähen lassen.',
    }

    app = App(root, translation_table)

    root.mainloop()

main()
Viel Spass beim Häkeln.
Gruss wuf ;-)
Take it easy Mates!
Antworten