Problems with Tkinter window crashing in Python 3 - python-3.x

I've been developing a chatbot sort of program with tkinter and sockets, and I've had problems integrating Tkinter with the chatbot system, after clicking the "Ask" button, the tkinter window stops responding.
Code for the Client:
`
import socket, threading, time
import tkinter as tk
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '25.65.227.4'
port = 10001
window = tk.Tk()
window.geometry('800x400+20+20')
window.title('Client')
labelBot = tk.Label(window, text='')
labelBot.config(font='Decker')
labelBot.place(x = 10, y = 10)
e1 = tk.Entry(window)
e1.config(font='Decker')
e1.place(x=50, y=350)
photo = tk.PhotoImage(file="Aldenv1.gif")
labelPic = tk.Label(image=photo)
labelPic.place(x=450, y=10)
s.connect((host, int(port)))
print('connected to server')
labelBot['text'] = 'Connected to Server'
def Main():
while True:
data = s.recv(2048).decode('utf-8')
labelBot['text'] = ''
labelBot['text'] = data
if not data:
break
def bt_click():
answer = e1.get().encode('utf-8')
s.send(answer)
Main()
bt = tk.Button(window, text='Ask', command=bt_click)
bt.config(font='Decker')
bt.place(x=400, y=350)
window.mainloop()
if __name__ == '__main__':
Main()`
I tried doing all sorts of things but I haven't managed to solve this crash

Related

tkinter e Adafruit IO Why the cycle while it is not working?

I've recently been using python....
I cannot understand why the while loop prevents the creation of the tkinter window.
If I move the while loop before the mainloop I display the tkinter window but the loop stops.
import tkinter as tk
import time
from Adafruit_IO import Client, Feed, RequestError
ADAFRUIT_IO_USERNAME = "***********"
ADAFRUIT_IO_KEY = "**********************"
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
loop_delay = 5
temp = 25
try:
temperature = aio.feeds('temperature')
except RequestError:
feed = Feed(name="temperature")
temperature = aio.create_feed(feed)
def sendtemp(temp):
aio.send_data(temperature.key,temp)
data = aio.receive(temperature.key)
print(data.value)
window = tk.Tk()
window.title ("Thermometer")
window.geometry("300x100")
label = tk.Label(window, text = temp)
label.pack()
window.mainloop
while True:
sendtemp(temp)
time.sleep(loop_delay)
I solved it this way, what do you think?
import tkinter as tk
from Adafruit_IO import Client, Feed, RequestError
ADAFRUIT_IO_USERNAME = "**********"
ADAFRUIT_IO_KEY = "***************"
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
try:
temperature = aio.feeds('temperature')
except RequestError:
feed = Feed(name="temperature")
temperature = aio.create_feed(feed)
class Timer:
def __init__(self, parent):
self.temp = 25
self.label = tk.Label(text="--,- °C", font="Arial 30", width=10)
self.label.pack()
self.label.after(5000, self.sendtemp)
def sendtemp(self):
aio.send_data(temperature.key,self.temp)
data = aio.receive(temperature.key)
print(data.value)
self.label.configure(text="%i°C" % self.temp)
self.temp +=1
self.label.after(5000, self.sendtemp)
if __name__ == "__main__":
window = tk.Tk()
window.title ("Thermometer")
window.geometry("300x100")
timer = Timer(window)
window.mainloop()
Your while loop is blocking the program. It literally does nothing but calling sendtemp and sleeping.
while True:
sendtemp(temp)
time.sleep(loop_delay)
There is no room for reacting to events on the TK window, Python is busy doing those two things.
To do actions periodically, you need to set up a timer that runs on TK's main event loop (the one you start with .mainloop()). This is done with the .after() method.
This method takes a millisecond delay and a function you want to call.
window = tk.Tk()
window.title ("Thermometer")
window.geometry("300x100")
timer_id = None
timer_delay = 5000
def sendtemp(temp):
aio.send_data(temperature.key,temp)
data = aio.receive(temperature.key)
print(data.value)
if timer_id is not None:
start_timer()
def start_timer():
global timer_id
timer_id = window.after(timer_delay, sendtemp)
def stop_timer():
global timer_id
if timer_id is not None:
window.after_cancel(timer_id)
timer_id = None
start_timer()
window.mainloop()
You could bind stopping and starting the timer to a button.

Can't get a connection between two socket scripts

I am trying to build a tkinter messaging app using the socket module in python, I was testing it, but two copies of the script cannot connect to each other (one hosting and the other connecting). The create_messaging_screen() method never runs.
import socket
from threading import Thread
import tkinter as tk
default_port = 43777
self_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def client_link():
self_socket.connect(('', default_port))
create_messenging_screen()
self_socket.sendall(("hi to you too").encode("utf-8"))
while True:
try:
print(self_socket.recv(1024))
except:pass
def host_link():
self_socket.bind(('', default_port))
self_socket.listen(1)
global client_conn
client_conn, client_addr = self_socket.accept()
create_messenging_screen()
client_conn.sendall(("hi").encode("utf-8"))
print(client_addr)
while True:
try:
print(client_conn.recv(1024))
except:pass
def continue_setup(route):
if route == None:
Thread(target=host_link).start()
else:
Thread(target=client_link).start()
def create_messenging_screen():
#clear the window and create the messaging GUI
window = tk.Tk()
#IM app connection screen
#host a chatroom
button(window, command=lambda: continue_setup(None))
#join a chatroom
button(window, command=lambda: continue_setup(1))
window.mainloop()
acw1668 was correct, I should've used 'localhost' or something of the like for the IP parameter in self_socket.connect('', default_port).

python 3+ with tkinter: modal window not appearing

I'm new to python. I'm trying to set up a GUI with a main window and a modal window to receive the database credentials. The main window has to be in the background when the modal window appears and the user should not interact with it. Actually the application should terminate in case the user fails to provide a valid username and password. I'm presenting part of the code here. What I get is the main window (the modal window does not appear at all) and I can't do anything but shut the program down - not directly; only through visual studio.
'''
import tkinter as tk
class login:
def checkDbCredentials(self, user, password):
if (len(user)<4 or len(password)<4):
return 0
return 1
def ok(self):
us = self.user.get()
pw = self.password.get()
if self.checkDbCredentials(us, pw)==0:
tk.messagebox.showinfo("Error", "Username and Password do not match")
sys.exit(0)
self.dic['user']=us
self.dic['pass']=pw
self.win2.destroy()
def cancel(self, event=None):
self.parent.focus_set()
self.destroy()
def widgets(self):
x = self.win2.winfo_screenwidth()
y = self.win2.winfo_screenheight()
xx = int((x-270)/2)
yy = int((y-85)/2)
geo = "270x85+" + str(xx) + "+" + str(yy)
self.win2.geometry(newGeometry=geo)
self.tempLab= tk.Label(self.win2, text="Username:", pady=5)
self.tempLab1 = tk.Label(self.win2, text="Password:")
self.iuser = tk.Entry(self.win2, textvariable = self.user, width=30)
self.ipass = tk.Entry(self.win2, textvariable = self.password, width=30, show="*")
self.tempLab.grid(column=0, row=0)
self.tempLab1.grid(column=0, row=1)
self.iuser.grid(column=1, row=0)
self.ipass.grid(column=1, row=1)
self.bt = tk.Button(self.win2, text="Submit", command=lambda: self.ok())
self.bt.grid(column=0, row=2, columnspan=2, pady=5)
self.win2.bind("<Return>", self.ok)
self.win2.bind("<Escape>", self.cancel)
def __init__(self, dic, parent):
self.win2 = tk.Toplevel(parent)
self.parent=parent
self.dic = dic
self.user = tk.StringVar()
self.password = tk.StringVar()
self.win2.overrideredirect(1)
self.widgets()
self.win2.transient(parent)
self.win2.grab_set()
self.win2.protocol("WM_DELETE_WINDOW", self.cancel)
self.parent.wait_window(self.win2)
And the main class code is the following:
import tkinter as tk
from tkinter import ttk, X, Y, BOTH
from tkinter import messagebox
import sys
import login
class mainWindow:
def connect(bt, fr):
notImplementedYet()
def notImplementedYet():
msg = tk.messagebox
msg.showinfo("Warning", "Not Implemented yet!")
btX = 20
btY = 5
btSunken = '#aec2c2' # sunken button color
btRaised = '#85adad' # normal button color
rw = 0 # starting row for buttons
cl = 0 # starting column
dbCredentials = {"user":"", "pass":""}
win = tk.Tk()
win.title("Title")
win.geometry(newGeometry="1366x700+00+00")
win.iconbitmap(bitmap="bitmap.ico")
fr1 = tk.Frame(win, width=1366, height=50, background='beige')
fr1.grid(column=cl, row=rw, rowspan=5)
fr2 = tk.Frame(win)
fr2.grid(column=0, row=1)
lb2 = tk.Label(fr2, text="frame 2", background=btRaised)
btConnect = tk.Button(fr1, text="Connect", background = btRaised ,command=lambda: connect(btConnect, fr1), width=btX, height=btY)
btConnect.grid(column=0, row=0)
log = login.login(dbCredentials, win)
win.wait_window(log.win2)
print(dbCredentials)
win.mainloop()
win = mainWindow()
The question is why is the modal window not appearing? I've tried placing the 'wait_window' in the other class as well but it's still not working.

How to use multithreading with tkinter to update a label and simultaneously perform calculations in a thread controlled by button events?

I'm trying to start a counter that displays the value in a label on a separate display window.
The main window has a START, STOP and DISPLAY button.
The START must start the counter, STOP must stop it and the display window must open only when I click on DISPLAY button.
Here's what I have so far. The buttons seem to be unresponsive and the display window pops up without user intervention. How can I fix this?
import tkinter as tk
import time
import threading
import queue
def Run_Device(disp_q,flaq_q):
temp_q = queue.Queue()
temp_q.put(0)
while(flaq_q.empty()):
#time.sleep(0.2)
count = temp_q.get()
count += 1
temp_q.put(count)
disp_q.put(count)
else:
flaq_q.queue.clear()
def P_Window(disp_q):
pw = tk.Tk()
value_label = tk.Label(pw, text=disp_q.get(), relief='sunken', bg='lemon chiffon', font='Helvetica 16 bold')
value_label.pack()
def update_values():
value_label.config(text=disp_q.get())
value_label.after(1000,update_values)
update_values()
pw.mainloop()
def Stop_Dev(flaq_q):
flaq_q.put("Stop")
if __name__ == "__main__":
disp_q = queue.Queue()
flaq_q = queue.Queue()
t_device = threading.Thread(target=Run_Device, args=(disp_q, flaq_q), name="Device 1")
t_disp = threading.Thread(target=P_Window, args=(disp_q, ), name="Display 1")
window = tk.Tk()
start_button = tk.Button(window, text='Start', command=t_device.start(), bg='spring green', font='Helvetica 12 bold', width=20, state='normal', relief='raised')
start_button.pack()
stop_button = tk.Button(window, text='Stop', command=lambda: Stop_Dev(flaq_q), bg='OrangeRed2', font='Helvetica 12 bold', width=20, state='normal', relief='raised')
stop_button.pack()
disp_param_button = tk.Button(window, text='Display', command=t_disp.start(), bg='sky blue', font='Helvetica 12 bold', width=20, state='normal', relief='raised')
disp_param_button.pack()
window.mainloop()
I'm trying to learn how to use multithreading in tkinter so any feedback would be appreciated
There are two issues I see with your code. The first is just simple bugs, e.g.:
start_button = tk.Button(window, text='Start', command=t_device.start(), ...
here it should be command=t_device.start otherwise the command is the function returned by t_device.start()
The second is you've not dealt with various "What if ...?" scenarios, e.g. What if the user pushes 'Start' or 'Display' multiple times?
I've tried to address the above in my rework below:
import tkinter as tk
from time import sleep
from queue import Queue, Empty
from threading import Thread
FONT = 'Helvetica 16 bold'
def run_device():
count = 0
while flaq_q.empty():
count += 1
disp_q.put(count)
sleep(0.5)
while not flaq_q.empty(): # flaq_q.queue.clear() not documented
flaq_q.get(False)
def p_window():
global pw
if pw is None:
pw = tk.Toplevel()
value_label = tk.Label(pw, text=disp_q.get(), width=10, font=FONT)
value_label.pack()
def update_values():
if not disp_q.empty():
try:
value_label.config(text=disp_q.get(False))
except Empty:
pass
pw.after(250, update_values)
update_values()
elif pw.state() == 'normal':
pw.withdraw()
else:
pw.deiconify()
def stop_device():
if flaq_q.empty():
flaq_q.put("Stop")
def start_device():
global device
if device and device.is_alive():
return
while not disp_q.empty():
disp_q.get(False)
disp_q.put(0)
device = Thread(target=run_device)
device.start()
if __name__ == "__main__":
disp_q = Queue()
flaq_q = Queue()
root = tk.Tk()
pw = None
device = None
tk.Button(root, text='Start', command=start_device, width=20, font=FONT).pack()
tk.Button(root, text='Stop', command=stop_device, width=20, font=FONT).pack()
tk.Button(root, text='Display', command=p_window, width=20, font=FONT).pack()
root.mainloop()
I've left out some details to simplify the example. Even with its additional checks, it's still nowhere near perfect. (E.g. it hangs if you don't 'Stop' before you close the window, etc.)
You might be able to adapt something like the following:
import tkinter as tk
import threading
import queue
import time
def run_device():
for i in range(4):
print(i)
pass
def process_queue(MyQueue):
"""Check if we got a complete message from our thread.
Start the next operation if we are ready"""
try:
# when our thread completes its target function (task),
# the supplied message is added to the queue
msg = MyQueue.get(0)
# check message
print(msg)
except queue.Empty:
# .get failed, come back in 100 and check again
print('no message')
threading.Timer(0.001, lambda q=MyQueue: process_queue(q)).start()
class ThreadedTask(threading.Thread):
"""threaded task handler"""
def __init__(self, queue, target, msg):
threading.Thread.__init__(self)
# message to add to queue when the task (target function) completes
self.msg = msg
# function to run
self._target = target
# queue to store completion message
self.queue = queue
def run(self):
"""called when object is instantiated"""
# start users task
try:
self._target()
except Exception as e:
self.queue.put('Thread Fail')
return
# we are done, pass the completion message
self.queue.put(self.msg)
if __name__ == '__main__':
MyQueue = queue.Queue()
MyThread = ThreadedTask(MyQueue, run_device, 'Thread Task: Run')
MyThread.start()
process_queue(MyQueue)

Python 3 - How do I use tkinter progressbar with a bat file?

So far, the bat runs but the progress bar doesn't. How do I connect the two with each other? Here is the image of the output.
http://imgur.com/lKbHepS
from tkinter import *
from tkinter import ttk
from subprocess import call
def runBat():
call("mp3.bat")
root = Tk()
photobutton3 = PhotoImage(file="smile.png")
button3 = Button(root, image=photobutton3, command=runBat)
button3.grid()
pbar = ttk.Progressbar(root, orient=HORIZONTAL, length=200, mode='determinate')
pbar.grid()
root.mainloop()
This answer ended up not working. The question is still open.
Try this:
import subprocess
import threading
import ctypes
import re
from tkinter import *
from tkinter import ttk
class RunnerThread(threading.Thread):
def __init__(self, command):
super(RunnerThread, self).__init__()
self.command = command
self.percentage = 0
self.process = None
self.isRunning = False
def run(self):
self.isRunning = True
self.process = process = subprocess.Popen(self.command, stdout = subprocess.PIPE, shell = True)
while True:
#Get one line at a time
#When read() returns nothing, the process is dead
line = b""
while True:
c = process.stdout.read(1)
line += c
if c == b"" or c == b"\r": #Either the process is dead or we're at the end of the line, quit the loop
break
if line == b"": #Process dead
break
#Find a number
match = re.search(r"Frame\=\s(\d+\.?(\d+)?)", line.decode("utf-8").strip())
if match is not None:
self.percentage = float(match.group(1))
self.isRunning = False
def kill(self): #Something I left in case you want to add a "Stop" button or something like that
self.process.kill()
def updateProgress():
progressVar.set(rt.percentage) #Update the progress bar
if rt.isRunning: #Only run again if the process is still running.
root.after(10, updateProgress)
def runBat():
global rt
rt = RunnerThread("mp3.bat")
rt.start()
updateProgress()
root = Tk()
photobutton3 = PhotoImage(file="smile.png")
button3 = Button(root, image=photobutton3, command=runBat)
button3.grid()
progressVar = DoubleVar()
pbar = ttk.Progressbar(root, orient=HORIZONTAL, length=200, mode='determinate', variable = progressVar)
pbar.grid()
root.mainloop()
Basically, there's a thread that reads the data from the process and makes it available to a function that updates the progress bar every so often. You didn't mention the output's format, so I wrote it to use a regular expression to search for the first number and convert it.

Resources