首页 > 解决方案 > 如何在python中打印变量“new_text”

问题描述

text_box 是一个 tkinter 文本框我正在尝试将 source_text 的字母转换为与所述字母的位置相对应的数字,使得 a 或 A = 1、b 或 B = 2 并将其打印到控制台

    def enter():
    #don't use 1 in place of 1.0
    source_text=text_box.get(1.0, END)
    

    def alphabet_position(text):

        dict = {'a':'1','b':'2','c':'3','d':'4','e':'5','f':'6','g':'7','h':'8','i':'9','j':'10','k':'11','l':'12','m':'13','n':'14','o':'15','p':'16','q':'17','r':'18','s':'19','t':'20','u':'21','v':'22','w':'23','x':'24','y':'25','z':'26'}
        new_text = text.lower(source_text)
        for i in new_text:
            if i in dict:
                new_text = new_text.replace(i, dict[i])
            print (new_text)

输入是这样调用的

btn = Button(root, text = "Enter", command = enter)

标签: python

解决方案


        dict = {'a':'1','b':'2','c':'3','d':'4','e':'5','f':'6','g':'7','h':'8','i':'9','j':'10','k':'11','l':'12','m':'13','n':'14','o':'15','p':'16','q':'17','r':'18','s':'19','t':'20','u':'21','v':'22','w':'23','x':'24','y':'25','z':'26'}
        new_text = text.lower()
        for i in new_text:
            if i in dict:
                return new_text.replace(i, dict[i])

def enter():
    print (alphabet_position(text_box.get(1.0, END)))

推荐阅读