Why Graphical effects doesn't change in PyQt5? - python-3.x

I am using PyQt5 and Python3 to write a program.
I am using QGraphicsBlurEffect to make a blur effect on the main widget when a pop up windows is shown.
The problem is that when the popup window is closed the blur effect is still on the main widget and it doesn't show as normal. How can I set it to normal after closing the popup window?(I set the graphicsEffect of mainwindow to None in closing event of popup window but it doesn't change anything).
This is the code:
main widget:
self.blureffect = QGraphicsBlurEffect(self) #defining the effect
def fontpgshow(self): #shows the popup window
self.setGraphicsEffect(self.blureffect) #puts the blur effect on the main widget
#the 3 bottom lines show the popup window
import fontwindow
self.fontpg = fontwindow.fontpage()
self.fontpg.show()
popup window:
def closeEvent(self,event):
event.accept()
#in the bottom lines I set the effect of main window to None
import settingswindow
self.setpg = settingswindow.settingspage()
self.setpg.setGraphicsEffect(None)

I solved my problem by changing the type of the pop up window from QWidget to QDialog and I used self.accept() and self.reject() in the QDialog for sending signals back to the main widget that here it was the setting page. After getting signal I set the graphics effect of the main widget to None and everything is working ok now.

Related

How to propagate the parent's click event in Pyside2 as if I have clicked the button directly while hovering over it?

How to propagate the parent's click event in Pyside2 as if I have clicked the button directly while hovering over it?
This is a very peculiar problem, and I haven't been able to find even a question like this.
A button is positioned on the main UI window. When clicked, it hides the parent window, and spawns another window at that position. I need it to work with a single click, for example, I want to press the button, and not release the mouse immediately, but hover over a certain part of the newly spawned window and then release it.
This works perfectly fine if I click the button directly , but the problem is I need to click the button from the main UI window.
I am doing it by sending "mousePressEvent" from the main UI window to the child widget's "mousePressEvent", like this:
def mousePressEvent(self, event): #parent mouse press event
#----
#----
self.FocusedButton.mousePressEvent(event) # child widget
When I activate the button from the mainUI window, the window is immediately hidden and the new window is spawned. However, since I haven't performed mouse release yet, I am stuck into the parent's mouse press event loop. The mouse doesn't invoke paint event and doesn't register mouseMoveEvent until I release the click because the mainUI window is hidden and mouse tracking doesn't work any more.
I have tried setting the flags of the window in every way, (QtCore.Qt.WA_TransparentForMouseEvents,True ) for example, but none of it worked.
The main UI window inherits from "QtWidgets.QWidget" and does not have the clicked method.

Adding a button to align with ttk notebook caused a geometry problem

I have found out a method to get a button to align with ttk.Notebook.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
notebook = ttk.Notebook(root)
ttk.Button(notebook).pack()
notebook.pack(fill='both', expand=1)
label = ttk.Label(notebook, text='Text', font='Arial 50')
notebook.add(label, text='Tab')
root.mainloop()
However, this caused a problem that the geometry manager only displays the button. I have to maximise the window in order to see all the contents.
In my another bigger gui, this is just a little bit of the window. I can’t see the content inside the notebook even I maximise the window.
So, how can I get it working? Thanks for any ideas!
This is due to the fact that you're calling pack on a button inside the notebook. The button is empty so it has a size of zero, and calling pack on the button causes the notebook to shrink to the size of the button.
You shouldn't ever call pack or grid on widgets that are direct children of a notebook, they should only ever be added with the add method of the notebook.
It's not clear where you want the button, but it shouldn't be a child of the notebook. If you make it a child of the root window (eg: ttk.Button(root), you can add it to the root window with pack to get it to be either above, below, or to one of the sides of the notebook.

How to fix tkinter cursor vertical offset while in full screen on macOS?

I am having a problem with Tkinter detecting the wrong cursor position when the application is in fullscreen mode on macOS.
I have created a Python 3 application with a Tkinter GUI on macOS (Mojave). The application works well until the green full screen button is pressed. After this the menu bar is hidden as expected, but the window only expands to 2560x1395 on a 2560x1440 display. The interface also detects the mouse cursor above where it appears on the screen, which makes the application impossible to use. I have tried forcing the application to expand to the full height of 1440, but it immediately changes back to 1395.
Here is a minimal case demonstrating the problem:
import tkinter as tk
def test():
print("Test")
root = tk.Tk()
tk.Button(root, text="Test", width=20, command=test).pack()
root.mainloop()
While in normal windowed mode, clicking the button causes "Test" to be printed. While in full screen the user must click below the button for the click to be registered. Exiting the application while in full screen mode also causes a segmentation fault.
In my application, clicking where the red dot is causes the OptionMenu to open:
The applications which support macOS properly opens in a separate Desktop tab when used in fullscreen. As far as I observed this is not case with tkinter, the window expands to fit completely with the screen size onto the main Desktop tab.
Why there is an offset when in fullscreen mode?
The offset is of the titlebar which hides in fullscreen mode but doesn't register and shifts all the widgets accordingly. That's why mouse clicks register + 24 (height of the titlebar) from the click. To avoid this we use root.attributes('-fullscreen', 1).
To exit fullscreen we can use <Escape> bind as Escape key in most application is to exit from fullscreen like in Safari. You can also use a Button.
root.bind('<Escape>', lambda e: root.attributes('-fullscreen', 0))
I couldn't find an event handler for fullscreen mode on Mac so I used <Configure> with condition root.attributes('-fullscreen') to check if the user clicked for fullscreen or not.
This is what I come up with.
from tkinter import *
root = Tk()
Button(root, width=20, text='Hello').pack()
def full_screen(evt=None):
# checks if the window is in fullscreen
if root.attributes('-fullscreen'):
root.attributes('-fullscreen',1)
# Remove the borders and titlebar
root.overrideredirect(True)
root.overrideredirect(False)
root.bind('<Configure>', full_screen)
# Escape bind to exit fullscreen.
root.bind('<Escape>', lambda e: root.attributes('-fullscreen', 0))
root.mainloop()
This issue is known for 6 months now, and Apple is doing nothing about it, now the fun fact is the logic display is fine (if you record your screen or if you screen share it doesn't happen) it only happens on the physical screen, and it only happens on MBP 16"
I am now over the fact that there is a hardware issue with the (some) MBP 16" video cards and Apple is just playing dumb.
If it helps, having your screen without scale or with "double" scale works fine, it's in the 3 intermediate scales that the issue presents itself

abaqus thread tkinter Tk window refreshing never

I have run into a problem durring abaqus programming with phyton 2.7.
I'm using Tkinter and making a window with a lot of controls on it.
My main problem is that durring my plugin window is opened, the user needs to click on abaqus menuitems, browse modells, etc. So using the main program while my plugin still works on screen.
If I do create my Tk window without thread, than when the user clicks on abaqus main windo functions while my plugin is opened, then abaqus will not respond or crash with "LoadlibraryA error 193"
(example: while plugin runs and user clicks on Viewvport menü/ViewPort Annotation Options then he/she wont be able to change tabs)
If i do create my Tk window inside a thread, then the al the Tk window controls will only responds the mouse events after I leave the Tk window with my cursor.
(example: I make 2 notebook page and after start i click on the not selected one. then nothing happens until my mous inside the Tk window, but as soon as i move it out, the click takes effect and the tab changes...)
The threaded version of my code:
import threading
class pilotDB(threading.Thread):
def shutdown_ttk_repeat(self):
self.root.eval('::ttk::CancelRepeat')
self.root.destroy()
def __init__(self):
import threading
threading.Thread.__init__(self)
def refresh(self):
self.root.after(50, self.refresh)
def tabpage(self):
import ttk
import sys
self.notebook = ttk.Notebook(self.root)
self.tabpage_tab1 = ttk.Frame(self.notebook,width=400,height=500)
self.tabpage_tab2 = ttk.Frame(self.notebook,width=400,height=500)
self.notebook.add(self.tabpage_tab1, text='Tab1')
self.notebook.add(self.tabpage_tab2, text='Tab2')
self.notebook.place(x=30, y=40)
def run(self):
import Tkinter
self.root = Tkinter.Tk()
self.root.protocol("WM_DELETE_WINDOW", self.shutdown_ttk_repeat)
self.tabpage()
self.root.after(1000, self.refresh())
self.root.mainloop()
app = pilotDB()
app.start()
app/pilotDB has no function named "start" (last line of the code posted). If I put a call to self.run() in init, and delete threading, then the program works as expected, i.e. opens a window and displays 2 tabs and the user can switch between tabs, all the time the mouse is in the window as is normal. Note also that the refresh() function does nothing but call itself repeatedly. You should try to find the offending code by first commenting the lines for the "WM_DELETE_WINDOW", shutdown_ttk_repeat and the call to execute the function tabpage() which leaves a basic window. Then uncomment one and run it, and repeat until you find the error.

Python Tkinter Clickable Text?

I'm wondering if there's a way to make clickable text in Tkinter. Maybe like you would see on a title screen of a game, and where you hover your mouse over the text and it changes color/hightlights itself. All I need the click to do is execute another function.
Are either of these things possible? Thanks!
you are looking for tkinter's events:
tk_widget.bind("<Button-1>",CALLBACK)
The call back needs to take an event argument which is a dictionary containing information about what triggered the event.
This can run into issues with widgets that overlap such as windows in a canvas or labels sometimes triggering the callback for the window behind it.
For hovering the mouse over a widget the event is called "<Enter>" and moving mouse out of widget region is called "<Leave>" for highlighting text effect, if you just want to capture a click anywhere on a window then on the root call root.bind_all("<Button-1>",CALLBACK)
source: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/events.html
example:
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
def change_case(event=None):
new_text = str.swapcase(lab["text"])
lab.config(text=new_text)
def red_text(event=None):
lab.config(fg="red")
def black_text(event=None):
lab.config(fg="black")
root = tk.Tk()
lab = tk.Label(root,text="this is a test")
lab.bind("<Button-1>",change_case)
lab.bind("<Enter>",red_text)
lab.bind("<Leave>",black_text)
lab.grid()
root.mainloop()
hope this helps :)

Resources