I am trying to use an external kv file for my program and it is not working. It goes up to a certain point then stops and gives me an error message. The version of Python I am using is 3.5 and the version of kivy I am using is kivy 1.9.1.
This is the code I used in the .py file:
Here is what it displays when it runs:
I don't understand what going on. I am open to elaborate more on this question if it is not clear yet.
In your build function you must return a widget. Instead you return a class of a widget (which is not a widget in itself). Replace Label by Label() which returns an instance of the class Label.
Related
I want to make a QT4 (using QT designer) dialog, that contains a part where a file has to be selected.
Now, I know QFileDialog exists, and I can program something that does what I want.
But can I also just do it in QT designer?
Is there some way to get a "file select" widget in QT designer?
Or, I remember these buttons, having the selected file as a title and a little arrow allowing the user to select another file by the QFileDialog?
So is there a ready made solution, or do I have to program it myself?
There is no file dialog available from the Qt designer as far as I know. But you can easily do it with a few lines of code.
Assuming you have a simple button called pushButton and the path should be stored in lineEdit.
def selectFile():
lineEdit.setText(QFileDialog.getOpenFileName())
pushButton.clicked.connect(selectFile)
[edit]Just wondering though, are you using KDE by any chance? If so, than you can use the KUrlRequester for this. It can easily be configured to support anything from files to urls to directories.
QFileDialog exists in QtGui. At least in my version 4.4 and probably much earlier too. I think the reason it is not in Designer is because it opens its own window instead of being a widget to place on another window.
The documentation from QTDesigner could be better and at least hint of its existence.
Instantiate it and run the show command. It comes right up and defaults to /.
import QtGui
self.fileDialog = QtGui.QFileDialog(self)
self.fileDialog.show()
You can use method getOpenFileName() in QFileDialog Class.
QFileDialog.getOpenFileName() will return the file path and the selected file type
I got this : ('C:/Users/Sathsara/Desktop/UI/Test/test.py', 'All Files (*)')
To get only the file path use QFileDialog.getOpenFileName()[0]
Sample code:
def selectFile():
print(QFileDialog.getOpenFileName()[0])
dlg.locationBtn.clicked.connect(selectFile)
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.
I tried to submit additional information but kept getting the following error:
Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.
It is because Tkinter is a wrapper around an embedded tcl interpreter. Creating an instance of Tk initializes this interpreter. Since StringVar instances and widgets must be created by and in the interpreter, they can't be created until the interpreter has been initialized.
I am working on QT Designer for the first time. I have created a combo box with IP address values in it a.b.c.d and p.q.r.s. I translated the .ui file created in QT Designer to .py file using pyuic4 tool, so that it can be used in Python project that I am developing in Visual Studio.
Now when I run my Python project, the form is opened properly, but on the selection of any value in combobox/dropdown, it isn't displaying the selected value at the top although it shows the value as selected/highlighted when I see the complete list by clicking on dropdown again. Python code doesn't seem to contain currentIndexChanged properties.
Is this usual? Do I have to write the code for such a basic functionality manually?
I tried to write it manually using the code below but this also doesn't seem to be working:
self.IPAddress_comboBox.currentIndexChanged['QString'].connect(self.handleChanged)
def handleChanged(self, text):
index= self.IPAddress_comboBox.findText(text,QtCore.Qt.MatchFixedString)
if index >=0:
self.IPAddress_comboBox.setCurrentIndex(index)
Here I selected the value 10.200.25.11 which is displayed as highlighted but when the dropdown closes it displays 10.200.25.10 at the top. (while on qt designer, preview works fine)
There should not be quotes around the signal argument type QString:
self.IPAddress_comboBox.currentIndexChanged[QString].connect(self.handleChanged)
I'm not sure why your combo box isn't displaying the selected value at the top; that functionality works for me. If you could provide an example, that might help.
Iam trying to run a simple sample code of Hello World which is as follows:
import kivy
kivy.require('1.8.0') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def build(self):
return Button(text='Hello World')
if __name__ == '__main__':
MyApp().run()
and i have saved this as hello.py which then i run by using kivy-1.8.0 which in turn gives me an error message titled Kivy Fatal Error and the rest of the description is in Chinese,Japanese or Korean language.
Is there a problem of graphics card? or
Is there any other way to a Kivy application
please help.
Please post the full terminal output from when the app is run.
I don't remember what the funny character message means (it's nonsense, not real text), but I think it indicates a too-low opengl version. The appearance of the garbled message is itself a bug that I think is fixed in kivy master.
Possible issues with kivy (and python):
Indentation issues: make doubly sure that your indents are consistent: they should all be the same. If you are using a tab, make sure it is a tab everywhere. If you are using multiple spaces make sure they are the same number everywhere.
A more kivy specific issue: try to comment out line 2 (kivy.require('1.8.0')). You may not have the correct version of kivy installed. Alternatively, decrease that number (1.0.6 should be more than enough for what you are doing in this simple application).
I don't know what the issue could be with the language you get the message in. Perhaps you installed some weird distribution, or selected some language pack that it is defaulting to.