简体   繁体   English

在使用 tkinter 的语言翻译器代码中,如何将 output 保存为 pdf?

[英]In the language translator code using tkinter, how to save the output as a pdf?

I am making a language translator app using tkinter using the following code:我正在使用以下代码使用 tkinter 制作语言翻译器应用程序:

import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
from googletrans import LANGUAGES
import translate

root = tk.Tk()

src_text = Text(root, font="arial 10", height=11, wrap=WORD, padx=5, pady=5, width=60)
src_text.place(x=50, y=120)

des_text = Text(root, font="arial 10", height=11, wrap=WORD, padx=5, pady=5, width=60)
des_text.place(x=600, y=120)


class Lang_Translator(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.label()
    
    src_text = Text(root)
    des_text = Text(root)
    
    def label(self):
        Label(root, text="Language Translator", font="italic 20").pack(side="top")
        Label(root, text="Select Language", font="arial 15").place(x=30, y=60)
        Label(root, text="Select Language", font="arial 15").place(x=600, y=60)


lang = list(LANGUAGES.values())
src_lang = ttk.Combobox(root, values=lang, width=30)
src_lang.place(x=200, y=63)
src_lang.set("english")

des_lang = ttk.Combobox(root, values=lang, width=30)
des_lang.place(x=770, y=63)
des_lang.set("hindi")


def trans():
    translator2 = translate.Translator(to_lang=des_lang.get())
    translated = translator2.translate(src_text.get(0.0, END))
    des_text.insert(0.0, translated)


def clear_all():
    des_text.delete(0.0, END)
    src_text.delete(0.0, END)


button_1 = Button(root, text="Translate", command=trans).place(x=510, y=160)
button_2 = Button(root, text="Clear", command=clear_all).place(x=510, y=220)

app = Lang_Translator(master=root)
app.master.title('Translator')
app.master.geometry('1080x350')
app.mainloop()

How do I save the output, the translated language, as a pdf file?如何将翻译语言 output 保存为 pdf 文件?

I tried to make a separate function to save pdf using fpdf package.我尝试使用 fpdf package 制作单独的 function 以保存 pdf。

def savepdf():
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=15)
    pdf.cell(200, 10, txt="Language Translator", ln=1, align='C')
    pdf.cell(10, 200, txt=str(), ln=2, align='L')
    pdf.output("Translated.pdf")

and tried to use the translated variable from trans() function.并尝试使用来自trans() function 的translated变量。

def trans():
    translator2 = translate.Translator(to_lang=des_lang.get())
    translated = translator2.translate(src_text.get(0.0, END))
    des_text.insert(0.0, translated)

But I am stuck on how to do that.但我坚持如何做到这一点。 So how do you save the output as pdf using this function?那么如何使用这个 function 将 output 保存为 pdf?

translated is a local variable inside trans() and it cannot be accessed elsewhere. translatedtrans()内部的局部变量,不能在其他地方访问。 However you can use des_text.get(1.0, "end-1c") instead:但是,您可以使用des_text.get(1.0, "end-1c")代替:

def save_pdf():
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=15)
    pdf.cell(200, 10, txt="Language Translator", ln=1, align="C")
    pdf.cell(10, 200, txt=des_text.get(1.0, "end-1c"), ln=2, align="L")
    pdf.output("translated.pdf")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何使用用户可以选择的语言编写翻译器 - How do you code a translator with language user can choose 使用python线程的语言翻译器 - Language translator using python threads 如何在python中制作语言翻译器? - How to make a language translator in python? 如何在 tkinter 中以 pdf 格式保存画布? - How to save the canvas in tkinter in pdf format? 如何在我的 tkinter 项目中创建一个保存按钮来保存输出? - How to create a save button in my tkinter project to save the output? How to fit the output of Python code in pdf while using R markdown? - How to fit the output of Python code in pdf while using R markdown? 如何保存使用Tkinter按钮运行的python函数的输出? - How can I save the output of a python function that I run using a Tkinter button? 我正在尝试使用谷歌翻译器并在我选择 output 语言英语或印地语时发生翻译,但当 select odia - i am trying to using google translator and translate it is happening when i choosing the output language English or Hindi but when select odia 有没有办法将 tkinter 画布保存为 pdf? - Is there a way to save tkinter canvas to a pdf? Tkinter文件目录保存output - Tkinter file directory to save output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM