[英]What am I doing wrong with the variables?
I wanted to make a little program to change the capitalization of sentences to make them sound very sarcastic.我想做一个小程序来改变句子的大小写,让它们听起来很讽刺。 I wanted to have it in a little frame and used this tutorial ( https://datatofish.com/entry-box-tkinter/ ).
我想把它放在一个小框架中并使用本教程( https://datatofish.com/entry-box-tkinter/ )。 I've extracted some of its code and implemented it, but I think I messed up.
我已经提取了它的一些代码并实现了它,但我认为我搞砸了。
The Shell constantly gives me errors about missing variable which I can't seem to find in the code. Shell 经常给我关于缺少变量的错误,我似乎在代码中找不到。 Can some of you find where I made the mistake?
你们中的一些人能找到我犯错的地方吗?
import random
import tkinter as tk
root= tk.Tk()
root.title('hOofDleTteRdiNGes')
canvas1 = tk.Canvas(root, width = 400, height = 300)
canvas1.pack()
entry1 = tk.Entry (root)
canvas1.create_window(200, 140, window=entry1)
def capi_sentence(sentence):
sentence.lower = entry1.get()
new_sentence = ''
number = 0 #Dummy number for tracking
for letter in sentence.lower():
if len(new_sentence)<2: #Creates the first two letter
random_number = random.randint(0,1) #This randomly decides if the letter should be upper or lowercase
if random_number==0:
new_sentence += letter.upper()
else:
new_sentence += letter
else:
if (new_sentence[number-2].isupper() and new_sentence[number-1].isupper() or new_sentence[number-2].islower() and new_sentence[number-1].islower())==True:
#Checks if the two letters before are both upper or lowercase
if new_sentence[number-1].isupper(): #Makes the next letter the opposite of the letter before
new_sentence += letter.lower()
else:
new_sentence += letter.upper()
else:
random_number = random.randint(0,1)
if random_number==0:
new_sentence += letter.upper()
else:
new_sentence += letter
number += 1 #Add one more to the tracking
label1 = tk.label(new_sentence)
button1 = tk.Button(text='hoofdletter shufflen', command=capi_sentence)
canvas1.create_window(200, 180, window=button1)
You have a few problems.你有几个问题。
You need to call root.mainloop()
at the end so a window will appear.您需要在最后调用
root.mainloop()
以便出现 window 。
Your capi_sentence
function takes an argument but the callback called when pressing the button does not give it one.您的
capi_sentence
function 接受一个参数,但按下按钮时调用的回调没有给它一个参数。
Use利用
button1 = tk.Button(text='hoofdletter shufflen', command=lambda: capi_sentence(entry1.get()))
which calls your function with the return value of entry1.get()
.它使用
entry1.get()
的返回值调用您的 function 。
The first line of capi_sentence is unnecessary (and doesn't really make sense anyway) after you properly pass in the text.在正确传递文本后,第一行 capi_sentence 是不必要的(无论如何也没有任何意义)。
You've used lowercase label
when creating the label.您在创建
label
时使用了小写 label。 You also need to pass in a parent widget (eg root) and pass in the text as the text
keyword argument.您还需要传入一个父窗口小部件(例如 root)并将文本作为
text
关键字参数传入。 Finally, you need to add it to the window by packing it.最后,需要通过打包的方式将其添加到 window 中。
label1 = tk.Label(root, text=new_sentence)
label1.pack()
Some misc tips:一些杂项提示:
You do not need == True
at the end of your conditional;你不需要
== True
在你的条件结束; if will already run if true.如果为真,if 将已经运行。
You can remove the explicit number
logic by using enumerate
, which returns a list of tuples (i, item), with item the original item in the list and i its index from 0 to N - 1. I've rewritten your for loop below with this (and other simplifications) so it's a bit easier to read.您可以使用
enumerate
删除显式number
逻辑,它返回一个元组列表 (i, item),其中 item 是列表中的原始项目,i 它的索引从 0 到 N - 1。我在下面重写了你的 for 循环有了这个(和其他简化),所以它更容易阅读。
for i, letter in enumerate(sentence.lower()):
maybe_upper = letter.upper() if random.randint(0, 1) else letter
if len(new_sentence) < 2: #Creates the first two letter
new_sentence += maybe_upper
else:
l2, l1 = new_sentence[i - 2 : i]
if (l1.isupper() and l2.isupper()) or (l1.islower() and l2.islower()):
#Checks if the two letters before are both upper or lowercase
new_sentence += letter.lower() if l1.isupper() else letter.upper()
else:
new_sentence += maybe_upper
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.