PyQt5 resizes tkinter window automatically - python

I am trying to open a web page using PyQt5 after a button press in tkinter window.
As soon as the new window opens, it resizes (downsizes in this case) the tkinter window permanently.
Minimal code required to reproduce this
import sys
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QApplication
from tkinter import *
from threading import Thread
class Web:
def __init__(self,url,title='',size=False):
self.title=title
self.url=url
self.size=size
self.app=QApplication(sys.argv)
self.web=QWebEngineView()
self.web.setWindowTitle(self.title)
self.web.load(QUrl(self.url))
if size:
self.web.resize(self.size[0],self.size[1])
def open(self):
self.web.show()
sys.exit(self.app.exec_())
def launch():
web=Web('https://www.example.com')
web.open()
root=Tk()
button=Button(root,text='open',command=lambda:Thread(target=launch).start())
button.pack(padx=100,pady=100)
root.mainloop()
Images for reference
Both the images have the same height.
I would like to know the reason and a way to prevent this.

I have figured it out myself. PyQt changes the dpi awareness which does not happen by default with tkinter. Due to which the tkinter window resized itself as soon as PyQt was launched in the same main loop.
Since I am on a windows machine, using this solved the problem.
ctypes.windll.shcore.SetProcessDpiAwareness(1)

Related

SetWindowFlags immediately closes the program

I am trying to run the following script inside a software called Anki. This is basically an addon (a plugin) that makes the program to be always on top. This is the MRE:
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget
import time
app = QApplication([])
window = QWidget()
window.show()
def start():
while True:
window.setWindowFlags(Qt.WindowStaysOnTopHint)
#window.show()
#app.exec()
time.sleep(1)
start()
This is the closer I could get to the actual error I'm getting. Whenever the program runs the line window.setWindowFlags(Qt.WindowStaysOnTopHint), it immediatly closes the window and becomes unable to reach window.show(). Since this is just a plugin to the base app, I assume all plugins are loaded AFTER the main window has opened and within the main window. So if you close the main window, all plugins also stop running. How can I prevent the main window from closing?
There should only be one QApplication, and you don't have to create a new QApplication since anki creates it. Nor is a while True necessary since it will block the eventloop and even less use time.sleep since it also blocks the eventloop.
import time
from aqt import gui_hooks
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget
window = QWidget()
window.setWindowFlags(window.windowFlags() | Qt.WindowStaysOnTopHint)
gui_hooks.main_window_did_init.append(window.show)

How to create Tkinter main menu

I am currently creating a quiz game in Tkinter. This game consists of a login page, the quiz, a graph to show results and a pong game. The quiz and login use the Tkinter library for the GUI, the graph uses MatPlotLib and pong uses pygame. Is there any way that I would be able to link all of these modules in the main menu, for example creating a Tkinter GUI and then assigning a command to each button to launch the respective python files?
EDIT: Adding code that I have tried.
from tkinter import *
import sqlite3
import sys
import time
import importlib
importlib.import_module('Quiz')
class Menu:
def __init__(self, master):
#Setting up the window
self.master = master
self.master.geometry("1350x800+50+50")
self.master.title("Main Menu")
self.quizLaunch = Button(self.master, text="Quiz", command = quizApplication)
self.quizLaunch.pack()
root = Tk()
Menu(root)
root.mainloop()
Yes, it is completely possible. You can do this by importing packages like what you do in normal codes. You will be needed to import the following libraries to embed matplotlib into tkinter.
import matplotlib
import pygame
from tkinter import *
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
I recently made a basic GUI application using pygame in the python script along with tkinter library.
It would be great if you could post your code.
You can find the complete guide at:
https://pythonprogramming.net/how-to-embed-matplotlib-graph-tkinter-gui/

Tkinter program consumes all RAM/CPU

I ran this code and the RAM in my computer with my processor looks like it's going to explode! What is the reason?
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import os
bloque1=Tk()
bloque1.title('Bloque1')
bloque1.config(bg="#1C1C1C")
bloque1.geometry("450x410")
barramenu=Menu(bloque1)
menubar=Menu(bloque1)
menubar.add_cascade(label="Actividades", menu=menubar)
menubar.add_command(label="Instrucciones")
menubar.add_command(label="Ayuda")
menubar.add_command(label="Cerrar", command=bloque1.quit)
bloque1.config(menu=menubar)
bloque1.mainloop()
You are adding a menu to itself. No doubt this is causing an infinite loop inside of Tkinter.
menubar.add_cascade(label="Actividades", menu=menubar)
That menu= attribute needs to be given another menu that will appear when you select that cascade entry from the menubar.

Python - What is tkinter's default window reference?

import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')
After the 'askquestion' window closes the tkinter window still remains.
I can resolve this by the following:
import tkinter.messagebox
top = tkinter.Tk()
a = tkinter.messagebox.askquestion('','hi')
top.destroy()
This destroys the window.
My question is:
Is there a way to destroy the window without creating a reference to it?
I tried:
import tkinter.messagebox
a = tkinter.messagebox.askquestion('','hi')
tkinter.Tk().destroy()
but that has no effect.
If you destroy the root window, Tkinter try to recreate one when you call askquestion.
Don't destory the root window. Instead use withdraw.
import tkinter.messagebox
tkinter.Tk().withdraw()
a=tkinter.messagebox.askquestion('','hi')

PyQt4: Stop Window from taking Focus

What I am trying to do is make an on screen keyboard.
To do this I need to stop the Program from taking focus away from other windows.
Here is the code I have that keeps the window on top.
import sys
from PyQt4 import QtGui, QtCore, Qt
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
app = QtGui.QApplication(sys.argv)
mywindow.show()
app.exec_()
(Note: Example from Keep Window on Top)
So what I want to do is add code to stop the window taking focus.
Thanks
Change focus policy of window and all of its contents QWidget::setFocusPolicy

Categories