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/ ). 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. 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.
Your capi_sentence
function takes an argument but the callback called when pressing the button does not give it one.
Use
button1 = tk.Button(text='hoofdletter shufflen', command=lambda: capi_sentence(entry1.get()))
which calls your function with the return value of entry1.get()
.
The first line of capi_sentence is unnecessary (and doesn't really make sense anyway) after you properly pass in the text.
You've used lowercase label
when creating the label. You also need to pass in a parent widget (eg root) and pass in the text as the text
keyword argument. Finally, you need to add it to the window by packing it.
label1 = tk.Label(root, text=new_sentence)
label1.pack()
Some misc tips:
You do not need == True
at the end of your conditional; if will already run if true.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.