When launching a PYQT QFileDialog the QComboBox for selecting the current directory is not the ideal widget in all scenarios. I'm interested in replacing the QComboBox with a QLineEdit while keeping everything else the same.
Any suggestions?
you should create a inherited class from QFileDialog and modify it like you want. Here are the documentation QFileDialog
To display a dialog the user to open a file:
objFile = QFileDialog.getOpenFileName(self, 'Open File', '/home/user',
'My file (*.hello);; All File (*.*)'
)
the parameter of getOpenFileName are: a title ("Open File"), the place of start loking for (could be None), and the diferent type file (this last is optional). This method returns str to de url of the file.
Related
Overview:
I need a way to have a printer dialog to print an image (a modern looking one is preferred)
Description:
I tried to find a way to print an image with a printer dialog and I found win32api.ShellExecute(0, "print", "test.jpg", 'None', None, 0)
But to make that work I need a wait after that and when the wait is over the window will close.. and I want it to close ONLY when the user has pressed print or closed the window...
Is there a way of doing that?
and it would be even better if you could link me to another module/way to have that printer dialog maybe a more modern one.. (not the PyQt5 one) because the win32api one looks kinda old school
Edit: So I found this piece of code -
from PySide2.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit
import sys
from PySide2.QtPrintSupport import QPrinter, QPrintPreviewDialog
myapp = QApplication(sys.argv)
window = QMainWindow()
printer = QPrinter(QPrinter.HighResolution)
previewDialog = QPrintPreviewDialog(printer, window)
previewDialog.exec_()
myapp.exec_()
sys.exit()
Source (I have minimized the code above as much as possible): https://codeloop.org/pyside2-print-preview-dialog/
But I don't know how to preview an image (this is using PySide2) please help
Acroding to the document of ShellExecuteA function, nCmdShow parameter is passed via calls to ShellExecute, so I tried every nCmdShow parameters in this function, but it didn't work like you want. It always close the print window after user has printed/closed the window.I suggest you use Print Spooler API Functions to write your own printing interface, so that you can wait for the user's further operation after printing.
for reference this is all using Pyqt5 and Python 3.6:
I've got a QStandardItemModel that is built from QStandardItems that are strings of the items in a zip (the model displays all the contents of a zipfile). I went with this choice as I can not cache the files locally, and my research shows that QFileSystemModel can not work on archives unless I unpack at least temporarily.
All items in the QStandardItemModel end in the correct extension for the file (.csv,.txt,ect), and I need to display the icon a user would see if they were looking at the file in windows explorer, however show it in the qtreeview (a user seeing content.csv should also see the icon for excel). On that note, this application is only running on windows.
How can I pull the extensions default system file icon, and set it during my setting of these items? Would I have to manually download the icons for my known file types and do this, or does the system store it somewhere I can access?
Here's some basic code of how I build and display the model and treeview:
self.zip_model = QtGui.QStandardItemModel()
# My Computer directory explorer
self.tree_zip = QTreeView()
self.tree_zip.setModel(self.zip_model)
def build_zip_model(self,current_directory):
self.zip_model.clear()
with zipfile.ZipFile(current_directory) as zip_file:
for item in zip_file.namelist():
model_item = QtGui.QStandardItem(item)
self.zip_model.appendRow(model_item)
You can use QFileIconProvider:
def build_zip_model(self, current_directory):
iconProvider = QtWidgets.QFileIconProvider()
self.zip_model.clear()
with zipfile.ZipFile(current_directory) as zip_file:
for item in zip_file.namelist():
icon = iconProvider.icon(QtCore.QFileInfo(item))
model_item = QtGui.QStandardItem(icon, item)
self.zip_model.appendRow(model_item)
I'm trying to display a folder selection dialog with tkinter in Python, using tkinter.filedialog.askdirectory.
The docs say there is an option called:
mustexist - determines if selection must be an existing directory.
I'd suppose it means by setting mustexist = False, I can enter any arbitrary non-existent path into the bar at the top or the bottom and when hitting "Select folder", it should just return this path without asserting that it exists. Doing so however, an error saying "path does not exist" still appears, preventing dialog return.
Code to reproduce:
import tkinter as tk
from tkinter.filedialog import askdirectory
root = tk.Tk()
root.withdraw()
print(askdirectory(mustexist=False))
(tested on Python3.6 + 3.7 on Windows)
Do I missunderstand something here? Or does it indeed just not work as it should?
I was wondering too and this is the exact behaviour in OSX:
If you don't set it at all, you get an option to create a new folder in form of a "new folder" button in the dialog.
If you set it to True, you don't get that button, but CMD-N still allows you to create a folder.
If you set it to False, you cannot create a folder anymore.
I want to start learning Kivy. I have been using Pycharm as my IDE for Python programming. How can I change the default python interpreter to a kivy interpreter so pycharm can recognise kivy codes?
I have installed kivy.app and created symlinks. I have also installed kivy using pip. In my python program, I have been able to successfully import App from kivy.app and it works. But when I write code to design a widget (in this case a Box Layout), Pycharm underlines in red and doesn't execute the code.
The following code works fine:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class Widget1(BoxLayout):
pass
class MyApp(App):
def build(self):
return Widget1()
if __name__ == "__main__":
MyApp().run()
But when I write the following code to design the Box Layout, it doesn't work. Pycharm underlines: Widget1, Button, text and Label (Error written is: Unresolved Reference)
<Widget1>
Button:
text: "Please click here"
Label:
text: "Button has not been clicked yet"
If all the codes work, after running, it's supposed to return a Box Layout split into two. One part is a clickable button with "Please click here" written on it and the other part is just a label with "Button has not been clicked yet" written on it. But now when I run, it just returns an empty Box Layout (no label, no button, no text).
Question 2
the Box Layout code (<Widget1>) still doesn't work.
Solution
1) kv Filename
Since you are not using Kivy Builder to load your kv codes/file, therefore you are loading kv codes/file by name convention. Make sure your kv filename is my.kv
Kv language » How to load KV
There are two ways to load Kv code into your application:
By name convention:
Kivy looks for a Kv file with the same name as your App class in
lowercase, minus “App” if it ends with ‘App’ e.g:
MyApp -> my.kv
If this file defines a Root Widget it will be attached to the App’s
root attribute and used as the base of the application widget tree.
By Builder convention: You can tell Kivy to directly load a string or a file. If this string or file defines a root widget, it
will be returned by the method:
Builder.load_file('path/to/file.kv')
or:
Builder.load_string(kv_string)
2) kv Rule Context
In your kv file, add : (full colon) after class rule, <Widget1>
Snippets
<Widget1>:
Kv language » Rule Context
A Kv source constitutes of rules, which are used to describe the
content of a Widget, you can have one root rule, and any number of
class or template rules.
The root rule is declared by declaring the class of your root
widget, without any indentation, followed by : and will be set as the
root attribute of the App instance:
Widget:
A class rule, declared by the name of a widget class between < >
and followed by :, defines how any instance of that class will be
graphically represented:
<MyWidget>:
Rules use indentation for delimitation, as python, indentation should
be of four spaces per level, like the python good practice
recommendations.
Question 1
But when I write code to design a widget (in this case a Box Layout),
Pycharm underlines in red and doesn't execute the code.
Solution
You have to install KV Language auto-completion file.
Download this file, PyCharm_kv_completion.jar.
At bottom-right corner of PyCharm’s Welcome window, click Configure -> Import Settings.
Select the jar file you just downloaded and PyCharm will present a dialog with filetypes ticked. Click OK.
Restart PyCharm for the changes to take effect.
Is there any way for me to set up sublime to automatically create a folder of the same name when I create certain files.
I create landing pages that all have a lp_ prefix to the filename, I would like to watch for when a file with this name is created and then automatically create a folder of the same name in another directory (for css and images).
Would this be possible with a plugin or something like Grunt?
Example:
Create file: lp_test.php
Automate Folder Creation: /lp/lp_test/
You can make a plugin that extends EventListener and overrides (for example) on_post_save_async. You can use this simple example as base:
import sublime, sublime_plugin, os
# We extend event listener
class ExampleCommand(sublime_plugin.EventListener):
# This method is called every time a file is saved (not only the first time is saved)
def on_post_save_async(self, view):
variables = view.window().extract_variables()
fileBaseName = variables['file_base_name'] # File name without extension
path = 'C:/desiredPath/css/' + fileBaseName
if fileBaseName.startswith('lp_') and not os.path.exists(path):
os.mkdir(path)
EDIT: changed on_post_save to on_post_save_async as it runs in a different thread and does not block the application. Thanks MattDMo for commenting it and for adding python highlighting.