简体   繁体   English

如何在 Tkinter 中将 output 更改为小写?

[英]How do you change the output to lowercase in Tkinter?

I'm using Tkinter in python for the first time.我第一次在 python 中使用 Tkinter。 I'm trying to make the output the user has entered lowercase.我正在尝试将用户输入的 output 设为小写。 So if the enter "TEST" the output would be "test"因此,如果输入“TEST”,则 output 将是“test”

My code is:我的代码是:

from tkinter import *

# event functions (10:30 mins)
def onClickSubmitButton():                      # submit button event handler
        userEnteredText = textbox.get()         # get the enterd text from the Entry text box
        outputTextField.delete(0.0, END)        # (14:30 mins)delete all of the output field text contents
        outputTextField.insert(END, userEnteredText)  # (15:50 mins) output the user eneterd text




#main window (1:30 mins)
window = Tk()
window.title("Python Glossary")
window.configure(background="white")

# display text using - Label  (05:30 mins)
Label(window, text="Enter a string and press submit", bg="white", font="none 12 bold").grid(row=1,column=0,sticky=W) # using grid layout

# textbox for text entry - Entry (8:15 mins)
textbox = Entry(window, width=20, bg="white")
textbox.grid(row=2, column=0,sticky=W)          # grid position of textbox

# submit button - Button (9:30 mins) - calls onClickSubmitButton function when clicked
Button(window, text="SUBMIT", width=6, command=onClickSubmitButton ).grid (row=2,column=1, sticky =W)

#definitions - Label (11:50 mins)
Label(window, text="\n Your string", bg="white", font="none 12 bold").grid(row=4,column=0,sticky=W) # using grid layout

# output textField - Text(12:40 mins)
outputTextField = Text(window, width=75, height=6, wrap=WORD, background="white",)
outputTextField.grid(row=4, column=1,sticky=W)  # grid position of textField



# run the main loop
window.mainloop()

I tried:我试过了:

outputTextField.insert.lower(END, userEnteredText)

but that didn't work.但这没有用。 Any advice?有什么建议吗?

If you do outputTextField.insert(END, userEnteredText.lower()) then the entered text will be converted to lower case and then the insert function works as expected, taking the lowercase string as an argument.如果您执行outputTextField.insert(END, userEnteredText.lower())则输入的文本将转换为小写,然后插入 function 按预期工作,将小写字符串作为参数。

in the most cases of these string methods you need to know, that you need to make a new variable for it.在这些字符串方法的大多数情况下,您需要知道,您需要为其创建一个新变量。

def onClickSubmitButton():                      # submit button event handler
        userEnteredText = textbox.get()       # get the enterd text from the Entry text box
        low = userEnteredText.lower()
        outputTextField.delete(0.0, END)        # (14:30 mins)delete all of the output field text contents
        outputTextField.insert(END, low)  # (15:50 mins) output the user eneterd text

So here is a bit fancier option (I don't know if You need something like this but it also does what You ask for except no button has to be pressed to change characters to lowercase, in this case it will do that as the user is typing, try it out):所以这里有一个更漂亮的选项(我不知道您是否需要这样的东西,但它也可以满足您的要求,除了无需按下按钮将字符更改为小写,在这种情况下,它将作为用户执行此操作正在打字,试试看):

from tkinter import Tk, Text


def lower_input(event):
    if not event.char or r'\x' in repr(event.char):
        return
    text.delete('insert-1c')
    text.insert('insert', event.char.lower())


root = Tk()

text = Text(root)
text.pack()
text.bind('<Key>', lambda e: root.after(1, lower_input, e))

root.mainloop()

Basically it just binds the "<Key>" event to the text widget which means that when any key is pressed (and the focus is on the text widget) the event will be triggered which will then call the given function.基本上它只是将"<Key>"事件绑定到文本小部件,这意味着当按下任何键(并且焦点位于文本小部件上)时,将触发事件,然后调用给定的 function。 Also .bind passes an event argument so that should be handled even if not used however here it is used to detect the character or to stop the function if the pressed key does not have a character (eg "Caps Lock" or "Backspace" (which actually has a bytes type thingy so that is why there is also the other comparison of r"\x" in repr(event.char) )). .bind还传递了一个事件参数,因此即使不使用也应该进行处理,但是在这里它用于检测字符或在按下的键没有字符时停止 function(例如“Caps Lock”或“Backspace”(它实际上有一个字节类型的东西,所以这就是为什么r"\x" in repr(event.char)的其他比较。 Then it just deletes the just written character and places a new one that is in lower case in its place.然后它只是删除刚刚写入的字符并在其位置放置一个小写的新字符。 The root.after is used because the event is called before the character is typed to the Text widget meaning incorrect indexes are used so there is a tiny delay (1 ms) so that the character can appear on the screen and then the indexing happens.使用root.after是因为在将字符输入到 Text 小部件之前调用了该事件,这意味着使用了不正确的索引,因此存在一个微小的延迟(1 毫秒),以便字符可以出现在屏幕上,然后进行索引。

EDIT: it is also possible to add or event.char.islower() (if that doesn't work could also try or not event.char.isupper() ) to that if statement to increase the performance a little bit since it won't go through the replacing process if the character is already lowercase编辑:也可以在 if 语句中添加or event.char.islower() (如果这不起作用也可以尝试or not event.char.isupper() )以提高性能,因为它赢了't go 如果字符已经是小写,则通过替换过程

EDIT 2: as per PEP8 You should use snake_case for naming variables and functions and NOT camelCase编辑2:根据PEP8你应该使用snake_case来命名变量和函数,而不是camelCase

Useful sources/docs:有用的来源/文档:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM