How to modify Page Py output - python

I just moved from Matlab to Python. So I am looking hopefully to re-build my GUI in Matlab Guide with Page Python (only hopeful for better performance for big data)
I designed several Push Buttons and inside the code I try to write the below code just beneath Button1.configure.
I can not take out seop value from this clicked () function although I even define it as global variable. I need seop for whole the program.
self.Button1.configure(text='''Data''')
def clicked():
global seop
from tkinter import filedialog, messagebox
fname = filedialog.askopenfilename (initialdir="C:\Sgty")
import numpy as np
seop = np.loadtxt (fname)
messagebox.showinfo ('Data Import', 'Int')
s1 = self.Button1.configure (command=clicked, text="Import Data")

Related

How to fix Tkinter dead frezzing or crashing because of huge data?

I am trying to make a program to display one single image (.png extension) at once but giving a button to the user to change the picture.
What I have done is:
Reading the Image from my directory with help of Pillow module
Appended it to a list
With a button I increase or decrease the index of the list.
(Note I have to read 600 images approx.)
Here's the code:
import os
from tkinter import *
from PIL import ImageTk,Image
import threading,time
#Define the tkinter instance
x=0
win= Tk()
dir_path= os.path.dirname(os.path.realpath(__file__))
print(dir_path)
l1=[]
#Define the size of the tkinter frame
win.geometry("700x400")
def start():
threading.Thread(target=bg).start()
win.after(5000,threading.Thread(target=fg).start())
#Define the function to start the thread
def bg():
print("bg")
for i in range(1,604):
a=Image.open(f"{dir_path}\\{i}.png")
a=a.resize((500,700), Image.ANTIALIAS)
b=ImageTk.PhotoImage(a)
l1.append(b)
print(b)
print(len(l1))
def fg():
def add():
global x
x+=1
img2=l1[x]
d.configure(image=img2)
d.image = img2
d.update()
global d
d=Label(win,image=l1[x])
d.pack()
Button(win,text="add",command=add).place(x=0,y=0)
label= Label(win)
label.pack(pady=20)
#Create button
b1= Button(win,text= "Start", command=start)
b1.pack(pady=20)
win.mainloop()
But the problem is that the Tkinter gets dead freeze and laggy to an extent that the GUI is not operable.
So my question is,
How to fix Tkinter dead Frezzes and if there is any way to read the images as fast as possible?
The freezes of Tkinter depends on reading speed of the interpreter, because:
continuously reading and Showing the pictures are a huge job for Python.
reading images using opencv or any other image processing module wont help as reading part can become faster, but showing the image in tkinter is done using python only, so rapid changes in Label will cause tkinter to crash.
Solution:
Switch to a different compiler based language for example c++.
this solution is specifically for a image slideshow,
The solution is to use selenium with python. You can specify the image location in driver.get(image_path) and it will open the image for you, and if you want to change the image with a button, then just rename all your images with the numbers and make a button to add +1 to the index.

How to add another program which do manipulation in imported Excel sheet using Tkinter program

I have created two python programs. One in which I am doing all the manipulations like sorting the data in Excel file using xlrd and xlwt. Another is I created a GUI using Tkinter and importing the original Excel file on which I need to do the manipulations
My question is how to add this program for manipulation in Tkinter program so that on click I will get required file with all the manipulations done. Both the programs are working individually
Use the import keyword:
Put both files in the same directory, name them like variables (the name does not start from a number, contain dashes etc. Example: gui.py and operations.py)
Put everything from the operations file (except imports) in a function. Example:
import random
def main():
for x in range(10):
print(random.randint(1,10))
Use the import keyword:
from tkinter import Tk, Button
import operations
tk = Tk()
Button(tk, command=operations.main).pack()
tk.mainloop()
where operations (twice) is the name of your file with a function, minus the .py part, and main - the name of a functon.
There's a different way, a bad one, that's os-specific but does not require the main function. Depending on the OS you could try:
import os
os.system('python3 operations.py')# variation 1
os.system('python operations.py')# variation 2
os.system('py -3 operations.py')# variation 3
Hope that's helpful!

Tkinter text widget - Why does INSERT not work as text index?

I have a problem that annoys me. I am currently building a small app with a Tkinter GUI.
On the front page, I want some introductory text in either a text or a scrolledtext widget. The code examples I've come across uses keywords such as INSERT, CURRENT and END for indexation inside the widget.
I have literally copy pasted the below code into my editor, but it doesn't recognise INSERT (throws error: "NameError: name 'INSERT' is not defined"):
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
I can get the code to work if I change [INSERT] with [1.0], but it is very frustrating that I cannot get INSERT to work, as I've seen it in every example code I've come across
Use tk.INSERT instead of only INSERT. Full code is shown.
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(tk.INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
You don't need to use the tkinter constants. I personally think it's better to use the raw strings "insert", "end", etc. They are more flexible.
However, the reason the constants don't work for you is that you're not directly importing them. The way you're importing tkinter, you need to use tk.INSERT, etc.
INSERT could not be used directly.
You can use it in the past just because you used this in the past:
from tkinter import * # this is not a good practice
INSERT,CURRENT and END are in tkinter.constants.Now in your code,you even didn't import them.
If you want to use them,you can use
from tkinter.constants import * # not recommended
...
txt.insert(INSERT,'You text goes here')
Or
from tkinter import constants
...
txt.insert(constants.INSERT,'You text goes here') # recommend
If didn't want to import them,you can also use:
txt.insert("insert",'You text goes here')
Edit:I found in the source code of tkinter,it had import them,reboot's answer is also OK.

Python Tkinter Label Refresh Woes

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.

How to execute external script by using button widget?

I´m trying to execute an external script (named EC.py) by pressing a button on my GUI (named BEN.py) and I want it to be inserted on a list (list1) in my GUI.
My external script (EC.py) is like this:
import scipy
import numpy as np
from scipy import misc
from scipy import ndimage
I = scipy.misc.imread('lena.jpg').astype(int)
J = (I/10)*10
K = J + 10
Print K
and my GUI (BEN.py) code is:
import os
import Tkinter as tk
import ttk
def Execute():
EC.K
list1.insert(END, K)
my button widget:
mybutton = Button(myGUI, text=”Execute Code”, command = Execute).pack()
my output list:
list1 = Listbox(myGUI, height=20, width=80)
Everything goes reasonably fine, except that just by running my GUI code it already reads the EC.py script, before I press the button. What I want is to get this script to run only when I press the button.
I suppose (because you didn't wrote complete source code) you wrote import EC
before calling EC.K
Of course Python produce EC.pyc (compiled version) at startup to optimise multi module imports and validate the syntaxe of all implied modules.
What you want in a way is to have dynamical generated code. EC.py
I don't focused on security issues of this behaviour but the simplest way is to use:
try: execfile ("EC.py")
except Exception,msg:
print msg
raise

Categories

Resources