I want to access the spw
and wps
variable outside the stop_show()
can someone please help me on this. I want to import this variable into another function from a different module.
from tkinter import *
import time
para = "India is one of the youngest superpowers in the world.\n The National bird of India is the peacock, which has\n a very colourful and beautiful tail. The national flower of\n India is Lotus. Lotus comes in many colours, white and pink \nbeing prominent.The National animal of India is the Royal\n Bengal tiger."
words = len(para.split())
# Stop watch code starts:
def start():
global start_time
start_time = time.time()
def stop_show():
global end_time
end_time = time.time()
global elapsed_time
elapsed_time = end_time - start_time
print(f"Time elapsed: {round(elapsed_time, 2)}")
wps = words/elapsed_time
spw = elapsed_time/words
print(f"Your current WPS (Words per second): {round(wps, 2)}")
print(f"Your current SPW (Seconds per word): {round(spw, 2)}")
# UI code:
root = Tk()
para_label = Label(root, text=para, bg="red",
font="comicsansms 23 bold", width=100, height=10)
para_label.pack(fill=X)
Start_btn = Button(root, text="Start", command=start,
fg="red", width=25, height=5)
Stop_btn = Button(root, text="Stop", fg="red",
width=25, height=5, command=stop_show)
Start_btn.pack(anchor=S, side=TOP)
Stop_btn.pack(side=TOP, anchor=S)
frame = Frame(bg="grey", )
root.title("Readzy")
root.mainloop()
CodePudding user response:
You have 2 options here, first is to make it class based and modify the attribute of the class. Second is easier, just make it global variable and use global keyword.
wps = 0
spw = 0
def stop_show():
global end_time
global wps
global spw
end_time = time.time()
global elapsed_time
elapsed_time = end_time - start_time
print(f"Time elapsed: {round(elapsed_time, 2)}")
wps = words/elapsed_time
spw = elapsed_time/words
print(f"Your current WPS (Words per second): {round(wps, 2)}")
print(f"Your current SPW (Seconds per word): {round(spw, 2)}")
def unrelated_func():
print(wps)
CodePudding user response:
You are missing frame.pack()
. And your widget is not anything for frame
. And because you cannot see grey background.
It is warning not to use from tkinter import *
.
Code using snippet from @WingedSeal:
import tkinter as tk
import time
para = f"India is one of the youngest superpowers in the world.\n The National bird of India is the peacock, which has\n a very colourful and beautiful tail. The national flower of\n India is Lotus. Lotus comes in many colours, white and pink \nbeing prominent.The National animal of India is the Royal\n Bengal tiger."
words = len(para.split())
# Stop watch code starts:
def start():
global start_time
start_time = time.time()
wps = 0
spw = 0
def stop_show():
global end_time
global wps
global spw
end_time = time.time()
global elapsed_time
elapsed_time = end_time - start_time
print(f"Time elapsed: {round(elapsed_time, 2)}")
wps = words/elapsed_time
spw = elapsed_time/words
print(f"Your current WPS (Words per second): {round(wps, 2)}")
print(f"Your current SPW (Seconds per word): {round(spw, 2)}")
root = tk.Tk()
frame = tk.Frame(bg="grey")
frame.pack()
para_label = tk.Label(frame, text=para, bg="red",
font="comicsansms 23 bold", width=100, height=10)
para_label.pack(fill=tk.X)
Start_btn = tk.Button(frame, text="Start", command=start,
fg="red", width=25, height=5)
Stop_btn = tk.Button(frame, text="Stop", fg="red",
width=25, height=5, command=stop_show)
Start_btn.pack(anchor=tk.S, side=tk.TOP)
Stop_btn.pack(side=tk.TOP, anchor=tk.S)
root.title("Readzy")
root.mainloop()
Output: