Textedit + CombBox + pyqt4 - python

I need help with a program. I need use a texedit + combobox.
I need open a txt and set it to my textedit ( i've alredy did it)
than i need use my cursor, to get some word, than use a specific thing in my combobox... for example Polarity, this box have only numbers, 1,-1,0
and with a button, i need save it on a new txt or other archive.
I have a menu bar with open,new,save and close. I can open a file modify it and save, but i want to save with a buttom, the word that i underline(with cursor) + the number i selected and saved for that word.... it's possible? how i can do it?
It's better using table widget? im new using pyqt4 :(
please help me... i dont know how to do this thing ..

The best way to get started with PyQt4 is to look at some working examples that are similar to the application you want to write. The PyQt4 source has lots of these - download it from here, and have a look in the examples directory.
In addition to this, there are numerous tutorials available that will help get you started. A good place to start looking is the PyQt4 wiki.
Once you've worked through a few tutorials, you will probably be able to achieve what you want quite easily. And if you get stuck on something, you can always come back and ask about it on SO (just make sure to ask a specific question, and post the code that you have so far).

Related

Applying LiveServer Logic for Tkinter File

The people who are familiar with the Live Server of VS Code, would have easily understood what is the main motive of this question.
But for others, here's the explanation:
Main motive of Live Server is to Automatically Reload Your Site on Save in web development! (Which get changed for python tkinter).
When ever I change something in my python file which contains tkinter code, the change should be reflected in the main window (the main window should not re-open to reflect the changes).
I have tried to search on web as well as on stack over flow, but all the results are for updating value in entry, label, buttons etc. But what I want is, the whole window should be updated when ever I change something in my main file, and the main window should not be reopened to do so. So in short, updating whole window without closing it, on every changes in the main file or automatically reload your program on save without reopening!
What have I tried?:
I tried to detect change in file using os.getsize which satisfied the first part of my question, but however I am not able to solve the second part i.e window should not be closed.
import os
main__tkinter_filename="myfile.py"
initial_filesize=os.path.getsize(main_tkinter_filename) # Getting size of the file for
# comparison.
while 1:
final_filesize=os.path.getsize(main_tkinter_filename)
if final_filsize<intial_filesize or final_filesize>initial_filesize:
webbrowser.open(main_tkinter_filename)
Example:
from tkinter import *
root=Tk()
root.mainloop
results in the below GUI:
If i have added a=Label(text='text')anda.pack() after root=Tk(), it should show me the label, and if i have removed the same code, it should remove them.
I will answer your question by the best of my understanding,
I have some (a few projects of my own, still way too limited) experience with flutter which has hot-reload feature (same as you described above, which you want with python, mainly tkinter), I recently switched to python for gui (Loved it!), so I would like to share my research here:
I was successfully able to set up hot-reload both with kivy (kivymd hot reload, which comes with watchdog and kaki, which works real-time), and with tkinter, while there is a hitch with the later, you will have to press Ctrl + R as to reload the tkinter window, but it works without having to re-run the python program, I will leave the link to the found resources here, hope it helps with your query!
To setup hot-reload with tkinter (requires Ctrl + R), please refer here.
To setup hot-reload with kivy/kivymd (real-time), which I personally prefer, you can find the official docs here.
To mention, I use the above on Manjaro (Arch linux) with pycharm, atom, but I have also tried and have made it run successfully on Windows 10 with vs code (worked like charm)
Hope I could be of help! If you face any problem regarding the same, please feel free to ask! Thanks!
After digging around I have finally found out a way to implement hot reload feature (which #Stange answers provides) but just updating the selected frame or code.
The basic idea is constanly reading the file and executing the selected code, and removing the object in a list which are meant to be removed.
# Live Checker.py
import keyboard
while 1:
if keyboard.is_pressed("Ctrl+r"):
with open('test.py','r') as file:
file_data=file.read()
file_data_start_index=file_data.find("'#Start#'")
file_data_end_index=file_data.find("'#End#'")
exec_command=file_data[file_data_start_index:file_data_end_index]
with open('exec_log.txt','w') as txt_file:
txt_file.write(exec_command)
Here I am constantly checking if if ctrl+r key is pressed, and if pressed
it reads the file,
writes the selected code from the file into a txt file.
I have specified the start and end of the code to be updated by #Start# and #End# respectively.
# Main.py
def check():
with open('exec_log.txt','r') as exec_c:
exec_command=exec_c.read()
if len(exec_command)==0:
pass
else:
print(exec_command)
exec('for i in root.winfo_children():i.destroy()\n'+exec_command)
print('exec')
with open('exec_log.txt','w') as exec_c:
pass
root.update()
root.after(100,check)
root.after(100,check)
And in the main file, i have added the above code which continusly check if exec_log.txt file has any changes, and if changes are there, then it executes them and all so destroys the widget specified in the remove_list.
This is just a temporary solution which in my case helps me to implement the hot reload feature in tkinter.

Code to listen for changes in a pywinauto Listbox

I'm using pywinauto to interact with the window of an old game - specifically the dedicated server GUI. I've been able to connect to it with:
app = Application(backend="uia").connect(path=r"C:\Program Files (x86)\___.exe")
Using a loop, I'm able to read each line in a Listbox in a specific window using:
for i in dlg.ListBox.descendants():
print (i.window_text())
The Listbox behaves as like a console output area and I've also been able to write commands to the Edit (input) field which appear in the Listbox.
All of this is great considering I didn't think it would be easy at all!
However, the last part of what I'd like to do is proving more difficult...
I want to listen for changes in the list box so I can react to them and write different responses back into the Listbox. So for example, when a player joins the server, I want my code to know that a new line has appeared in the Listbox (so I can write a custom welcome message to the Edit field).
I have tried to look at listener scripts but I feel a bit out of my depth and can't seem to find a simple code example that could help me accomplish this. Does anyone have any ideas where I could look or what I could use?
Even better... would anyone be able to provide some sample code for me to re-work?

PyQt5 Display Function Output in Text Edit [duplicate]

This question already has an answer here:
Add more than one line to a QTextEdit PyQt
(1 answer)
Closed 4 years ago.
I'm attempting to create a GUI for an application that reads lines of text from a file onto a Plain Text Edit in PyQt5. I'm very new to PyQt5 and created this GUI through the Designer in an attempt use PyQt5 to upgrade the GUI of an application I made using Tkinter. I know the names of the "widgets" I need to utilize, but I don't know the proper way to write and format the code. Below is an example of what I'm trying to do, but rather than display the text in the Text Edit, I am simply printing it out to the Python Shell. It works fine.
self.pushButton.clicked.connect(self.Display_Tweets)
def Display_Tweets(self):
readtweets = open("Tweetfile", "r")
tweetlist = readtweets.readlines()
for x in tweetlist:
print(x)
readtweets.close()
This prints to Shell what I want displayed in the text edit box when I click the button, so all I should need to do is tell it to display "x" on the Text Edit, rather than print it. Since I am new to PyQt5, I don't actually know the proper syntax for this, nor can I find any relevant up-to-date documentation.
So when I try and change the function to instead display the lines of the file on a Plain Text Edit widget I run into issues that I am pretty sure are because I am messing up the "grammar". The name of the Plan Text Edit widget is "plainTextEdit", but I'm not sure how to add text to it. If I run the code below the GUI closes when I press the button. Again the above code prints out to the Shell what I want displayed by the Text Edit, I just don't know how to get it into the Text Edit. I've tried many variations on what I've shown here, but they all end with the GUI just closing on me.
self.pushButton.clicked.connect(self.Display_Tweets)
def Display_Tweets(self):
readtweets = open("Tweetfile", "r")
tweetlist = readtweets.readlines()
for x in tweetlist:
self.plainTextEdit.append(x)
readtweets.close()
What I'm trying can be accomplished in tkinter if you have a Text Edit box called "DisplayTweets" using the code below. For tkinter you simply write the name of the text edit box, use the insert function, and then in the parantheses tell it where to add the line of text (in the code below I add it to the end of the text box) and then you tell it what to add. So I've got this working in Tkinter, I just want to know how to do the same thing in PyQt5 and I haven't been able to find a good resource yet.
def Display_Tweets():
readtweets = open("Tweetfile", "r")
tweetlist = readtweets.readlines()
for x in tweetlist:
DisplayTweets.insert(END, x)
readtweets.close()
I want to add that I'm not sure why the user "eyllanesc" keeps referring this as a duplicate question of how to add multiple lines in a PlainTextEdit. This is a question of how to add any line in a PlainText Edit as when I tried my GUI would close. Never did I have a problem of adding only one line to a PlainTextEdit. I get I'm new here, but I'm not going to let you try and walk over me because of it. There are many new users struggling to learn the same things I am and you are not helping by referring users to a different question than the one I am asking. Any new users please refer to the answers of the user "Lt Worf" instead of ellyanesc. Lt Worf answered the question and gave incredibly good resources along with his answer.
PyQt is just a generated wrapper around Qt, so you need to use the Qt documentation for all the Qt objects you use.
For setting text, you can use setText('blabla')
http://doc.qt.io/qt-5/qtextedit.html#setText
It supports both HTML for formatted text, or plain text.
To append text at the end, I do something like this
txt.moveCursor(QtGui.QTextCursor.End)
txt.insertHtml(data + '<br>')
txt.moveCursor(QtGui.QTextCursor.End)
if I want to use HTML, otherwise .append('blabla') also works.
The moving cursor part is not necessary for append, but it's nice because it will make your text box scroll down as you keep adding stuff.
Here is the answer for anyone else with a similar problem. LtWorf answered my question, and the documentation he sent had the answer within it. Obviously, I just answered the question for myself so I'm no expert, but I will answer in a way aimed at newbie idiots like myself. In my bad code where I write
self.plainTextEdit.append(x)
I fail to tell the computer what type of text to expect. Instead I need to write something like the code below, which tells it to append the output as plain text.
self.plainTextEdit.appendPlainText(x)
Or I can tell the computer to append the output as Html.
self.plainTextEdit.appendHtml(x)

Opening up a Drop-Down-Menu Tkinter

I have been implementing in a drop down menu (offically called a "Tkinter.OptionMenu") inside my application, that once clicked open will allow users to skip to certain parts of the form (like skipping to a certain chapter of a book/play). I want to be able to OPEN that drop down menu on an event i.e with code however as much as i have searched, it seems the very best i can do i to do a .focus_set() which is better than nothing, but all it does it place a faint outline around the widget. I would like it to fully open it up, just as if the user had clicked on it.
I'm pretty sure that i'm missing something simple, but help would be appreciated :)
Edit: Yes, i do have good reason to programatically open it, and even if i didn't it would still be nice to find out. I do not need access to the functions the list gives, but rather, i need the user to pick something from that drop down, and so i would like to open it for them

Python widget/cursor detection?

Beginner python learner here. I have a question that I have tried to Google but I just can't come up with the proper way to ask in just a few words (partly because I don't know the right terminology.)
How do I get python to detect other widgets? For example, if I wanted a script to check and see when I click my mouse if that click put focus on an entry widget on a (for example) website. I've been trying to get it to work in Tkinter and I can't figure out even where to begin.
I've seen this:
focus_displayof(self)
Return the widget which has currently the focus on the
display where this widget is located.
But the return value for that function seems to be some ambiguous long number I can't decipher, plus it only works in its own application.
Any direction would be much appreciated. :)
Do you mean inside your own GUI code, or some other application's/website's?
Sounds like you're looking for a GUI driver, or GUI test/automation driver. There are tons of these, some great, some awful, many abandoned. If you tell us more about what you want that will help narrow down the choices.
Is this for testing, or automation, or are you going to drive the mouse and button yourself and just want something to observe what is going on under the hood in the GUI?
>How do I get Python to detect other widgets?
On a machine, or in a browser? If in a machine, which platform: Linux/Windows (which)/Mac?
If in a browser, which browser (and major version)?
> But the return value for that function seems to be some ambiguous long number I can't decipher
Using longs as resource handles is par for the course, although good GUI drivers also work with string/regex matching on window and button names.
> plus it only works in its own application.
What do you mean, and what are you expecting it to return you? You should be able to look up that GUI object and access its title. Look for a GUI driver that works with window and button names.
Here is one list, read it through and see what sounds useful. I have used AutoIt under Win32, it's great, widely-used and actively-maintained; it can be called from Python (via subprocess).
Here are comparisons by the author of PyWinAuto on his and similar tools. Give a read to his criticisms of its structure from 2010. If none of these is what you want, at least you now have the vocabulary to tell us what would be...

Categories

Resources