Python Tkinter Label Refresh Woes - python

Im back again with another python issue.
A short while ago I wrote a console based program that pulls stats from a bitcoin miner on your local network. I've decided I'd like to turn it into a gui, and choose a combination of EasyGUI and Tkinter for my program.
My input boxes (ip, refresh rate,asic type) are all using EasyGUI, simply to save lines of code as Tkinter would take far more writing to accomplish the same. However, My actual results page is written using Tkinter as it allows me to refresh the displayed data at a user-defined interval.
My issue is this:
I had my program running happily, and then made some small ui tweaks (title, font, etc) and now after my most recent compile (using pyinstaller) I've noticed the stats (labels) don't update at all. I have looked over my code countless times now and cannot seem to find what is blocking the stats from changing at the defined intervals.
I am hoping someone with a fresh pair of eyes can help me find my stupid mistake, as it was running perfectly before these small additions.
Heres a cut-down version that still runs and produces the same issue:
import Tkinter as tk
from pycgminer import CgminerAPI
cgminer = CgminerAPI()
cgminer.host = 192.168.x.x
summary = cgminer.summary()
update = 1000
def L1(label):
def hashrate():
msg = "Your current GH/S = "
speed = msg , summary['SUMMARY'][0]['GHS 5s']
label.config(text=speed)
label.after(update, hashrate)
hashrate()
root = tk.Tk()
root.title("Eyes On Miner GUI V0.2")
label = tk.Label(root)
label.pack()
L1(label)
root.mainloop()
Full code on pastebin, in case you'd like to try to run it yourself. (python 2.7) Full Code

I ran this much of your code, substituting time() for the summary. It works in IDLE. From the console, either run with python -i program.py or add root.mainloop.
import tkinter as tk
from time import time
update = 1000
def L1(label):
def hashrate():
msg = "Your current GH/S = "
speed = msg , time()
label.config(text=speed)
label.after(update, hashrate)
hashrate()
root = tk.Tk()
root.title("Eyes On Miner GUI V0.2")
label = tk.Label(root)
label.pack()
L1(label)
If the problem is not with summary['SUMMARY'][0]['GHS 5s'], then there must be an incompatibility with either CgminerAPI or more likely with easygui. The latter is meant to replace tkinter, not be used together with it. If the code worked at first and then quit, then one of the additional functions you used must have triggered a conflict.

Related

Real Time Data with tkinter

I am looking for a simple way to display changing real time data in a GUI in python. I am connected to 2 devices and want to display data constantly (like 20 different values), and when I press a button I want to control the one device.
Unfortunately I fail already with the display of the data. For this I have looked at some tkinter tutorials and explanations.
My idea was to implement it with a config function and to overwrite the label continuously. As example how I wanted to display one value:
import tkinter as tk
from pydualsense import pydualsense
# connect to the device
dualsense = pydualsense()
dualsense.init()
# create a window
window = tk.Tk()
# function for updating data
def show_data():
global dualsense
data_label_output.config(text=dualsense.state.LX)
# showing the data as a lable
data_label_output = tk.Label(window)
data_label_output.grid(row=1, column=1)
show_data()
#### or different solution
# showing the data as a lable
data_label_output = tk.Label(window, comand=show_data)
data_label_output.grid(row=1, column=1)
window.mainloop()
Unfortunately, the value is displayed only once at the beginning and nothing changes after that.
Another problem:
When I press the button, I want to be able to control the one device. For this I have a while True loop that permanently checks if a button is pressed and then executes actions. As a separate program no problem, but how do I integrate this into the tkinter GUI? When I start this PyCharm always crashes.
I use PyCharm and Python 3.8
About simple and functional ideas I would be happy, also to other tools/modules etc., as long as you can easily and quickly implement the idea. It's only for a research project and the programming is only a means to an end.
You can use the after method in tkinter to run something after a short delay. The following code will run show_data once the GUI is ready and then again every 1000 milliseconds.
import tkinter as tk
from pydualsense import pydualsense
# connect to the device
dualsense = pydualsense()
dualsense.init()
# create a window
window = tk.Tk()
# function for updating data
def show_data():
global dualsense
data_label_output.config(text=dualsense.state.LX)
window.after(1000,show_data)
# showing the data as a lable
data_label_output = tk.Label(window)
data_label_output.grid(row=1, column=1)
window.after_idle(show_data)
window.mainloop()
This resolves the updating issue, I'm not sure what behaviour you want when you press the button but if you elaborate and explain, I might be able to help and update this answer.

How to simulate random key inputs from a pre-defined set of keys, key combinations? tkinter GUI / window not working

i'm a beginner in python, only having finished a course on it and currently working through a book.
A fairly simple project (and one of the few actually useful things i could think of) is an anit-AFK tool. I started googleing and found a few elements that i fried to combine.
So i "frankensteined" a script together by stitching different elements and my first test seem to work kinda fine, generally sort of doing what i want it to.
However i have a few problems:
1: tkinter GUI doesn't really work (i'm running the code directly from PyCharm); a window opens but it completely lacks the buttons or anything really. It's a blank window, that seems to have the dimension i gave it, but lacking everything else.
2: I don't know how to make it press random keys, from a set of pre-defined keys and key combinations.
My idea was assigning each key to a number, then having a random number generator pick a number, assigning it to a variable. All of that in a loop so that each time another loop decides it's time to press a key, the key (combination') is a new random one.
Now that i've explained the general idea, here's the code i have until now:
import pyautogui
from random import randint
from time import sleep
from tkinter import *
running = True
key = ["w", "a", "s", "d"] # this is the "base" keys
key_2 = [key[randint(0,3)], key[randint(0,3)] + "shift", "space"] # this is for combinations
def anti_afk():
while running:
pyautogui.press(key_2[randint(0,2)])
print("Test") # to easily test if the sleep is working working
sleep(randint(5,30))
def start():
running = True
def stop():
running = False
root = Tk()
root.title("Anti AFK Tool")
root.geometry ("500x500")
app = Frame(root)
app.grid
start = Button(app, text="Start anti-AFK", command=start)
stop = Button(app, text="Stop preventing AFK", command=stop)
start.grid()
stop.grid()
root.after(1000, anti_afk)
root.mainloop()
As i said above i'm a beginner and pretty much everything regarding tkinter and pyautogui isn't written by me but taken from THIS and THIS post right here. Also this is the very first time i use tkinter
So, when running it with only one key (W key in my case) it generally seems to work kinda fine. The GUI doesn't work beyond what i described above.
I'm programming on my MacBook and get the "spinning ball of death" in the application window. Generally i want the program do be running mainly on windows.
So the problem is that the random key selections seems to be not working like i want it to. Especially the (in this case) shift+key doesn't. Also the GUI of the program doesn't work.
Another idea i had was defining a function for each action seperately and then randomly calling that function. But the problem of how to call / use random functions / list elements is basically the same and the list solution seems much more flexible with less code.
Can you help me with this?

How can I run a python code opening a Tkinter window and not a shell's one?

This is my first question.
I have a python program that recognize voice and reply with voice.
I wish to add a little GUI for my program (it should have only an image on background and a button to quit the program)
I would like that when I launch my code from terminal, it opened a Tkinter window and at the same time the python program start.
I’m working on Mac Os.
I use speech_recognition package to recognize voice and I use NSS speaker to let my computer speak.
This is a example of my code:
import speech_recognition as sr
from AppKit import NSSpeechSynthesizer
#VARIABLES
L = sr.Recognizer() #LISTENING
nssp = NSSpeechSynthesizer #SPEAKING
S = nssp.alloc().init()
while True:
audio = L.listen(source)
s = L.recognize_google(audio, language="en-US")
if s == "hi":
S.startSpeakingString_("Hello!!!")
Where do I have to write the Tkinter instructions to make sure that when I run my code it opens only a Tkinter window (while my program goes on) and not a shell's one?
You'll find it difficult to introduce your GUI as your code has already been written, note that everything in Tkinter has to be stored in some sort of Label or Widget and so you can't just print what you already have onto the Tkinter screen.
Here is some code to create a basic Tkinter window. Try searching online and playing around with how to present your variables within said window
import tkinter
from tkinter import *
root = tkinter.Tk()
root.configure(background = "#66ffdd") #here you can use any hex color code or just leave it blank and configure as default
root.title("Voice Program") #use the name of your program (this is the window header/title)
root.geometry("800x500") #these are your window dimensions
welcome = tkinter.Message(root, text = "Welcome to my program")
button = tkinter.Button(root, text="This button", command=print("hello")) #here insert a function for the button to perform i.e quit
welcome.pack()
button.pack() #packing presents your variables to the window - you can also use other geometry managers like grid
This site is really useful for showing you what widgets are available and what you can do with them - try searching any issues or posting a more specific question in the future if you struggle.
http://effbot.org/tkinterbook/button.htm

Tkinter - How to get Keypress anywhere on window?

I am trying to get keypresses in Python (2.7.10), bit I had no luck with getch(), as ord(getch()) was returning 255 constantly, so I am now using Tkinter. (I believe that Tkinter is also cross-platform so that should help as i plan to run this script on a Linux device).
I need to be able to get keypresses, even if they are not pressed while the Tkinter window is not active.
Here is my code:
from Tkinter import *
import time, threading
x = "Hi!"
def callback(event):
x = "key: " + event.char
print(x)
def doTk():
root = Tk()
root.bind_all("<Key>", callback)
root.withdraw()
root.mainloop()
thread1 = threading.Thread(target=doTk)
thread1.deamon = True
thread1.start()
I am not reveiving any errors, it is not not registering keypresses. I have also tried this without using threading, but it still does not work.
Please also note that I cannot use raw_input() as I need this to be able to run in the background and still get keypresses.
I am aware that this does not produce a frame, I do not want it to.
Thanks in advance for any help :)
PS: I have looked to other answers on StackOverflow and other sites, but they all either don't work or give solutions where keypresses are only registered when the tkinter frame is active.

How can I spawn multiple tkMessageBox.showerror at the same time?

I am creating a little time management tool, using Tkinter, so I can keep on task at work. I am having trouble with one aspect that I cannot seem to get working. I'm using the error box so that it is displayed in front of all other windows.
As it is now, the program starts a new thread on a function that keeps track of time, and compares it to the time the user entered for their task. Once real time > the time entered by the user, it starts another thread to spawn the tkMessageBox. I have tried this without starting a new thread to spawn the tkMessageBox, and the problem is the same. If the user enters the same time for 2 separate tasks, the error pop up freezes. I'm having trouble finding information on this topic specifically... The behaviour is odd because if I have 2 alerts, lets say 1 at 0600 and one at 0601, but I do not close the first error box that pops up and let it stay up until the second alert triggers, the second alert will just replace the first one(I would like multiple error boxes to pop up if possible). It's only the alerts that have the same trigger time that cause the pop up to freeze though.
This is my first GUI program and only started learning the concept of threading, and GUIs in the past 24 hours, so I'm not sure if this is a problem with threading or the tkMessageBox. Because of the behaviour of the error box, I’m thinking it is the thread module combined with the tkMessageBox module. The command I'm using is:
tkMessageBox.showerror('TIMER ALERT!!!', comp_msg)
Here is the source I put comments in there to help. The tkMessageBox I’m talking about is line 56.
I guess I'm not sure if I can even do what I am trying to do with the pop-up box, if I can, I'm not sure how. If I can't, is there a alternative way to spawn multiple error type pop-up boxes with Tkinter? I just want multiple boxes to be able to appear at any given time.
Thanks in advance, and I really appreciate any help at all.
EDIT:
import thread
from Tkinter import *
#Spawns Error Box. Runs in it's own thread.
def message_box(comp_msg,q): # q is an empty string because of thread module.
print "Spawning Error Box..."
eb =Toplevel()
eb.config(master=None,bg="red")
pop_l = Label(eb,text="ALERT!!!")
pop_l2=Label(eb,text=comp_msg)
pop_l.pack(pady=10,padx=10)
pop_l2.pack(pady=15,padx=10)
return eb
thread.start_new_thread(message_box,(comp_msg,""))
tkmessageBox default dialog boxes are modal. You could implement a simple none modal dialog box for this application. Here is a good document about creating custom dialog boxes.
This way you can create as many new custom dialog boxes as your app requires, since each one is just a new Toplevel.
Here is a simple Tkinter app that shows the clock on the main window. When you click on the button it starts new tkMessageBox dialog boxes in new threads. (If you run it) You could see that the main thread that runs the TK event loop is working (since the time is getting updated), but the error boxes are not showing up as expected.
#!/usr/bin/env python
import datetime
import threading
from Tkinter import *
import tkMessageBox
class MyApp(Frame):
def __init__(self, root=None):
if not root:
root = Tk()
self.time_var = StringVar()
self.time_var.set('starting timer ...')
self.root = root
Frame.__init__(self, root)
self.init_widgets()
self.update_time()
def init_widgets(self):
self.label = Label(self.root, textvariable=self.time_var)
self.label.pack()
self.btn = Button(self.root, text='show error', command=self.spawn_errors)
self.btn.pack()
def update_time(self):
self.time_var.set( str(datetime.datetime.now()) )
self.root.after(1000, self.update_time)
def spawn_errors(self):
for i in range(3):
t = threading.Thread(target=self.show_error)
t.start()
def show_error(self):
now = datetime.datetime.now()
tkMessageBox.showerror('Error: %s' % (str(now)), now)
if __name__ == '__main__':
app = MyApp()
app.mainloop()

Categories

Resources