How to add new label in window? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a window. There is a button. When the user clicks on this button, in the window must disappear button and appear new label and new button.
Do you understand?
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
def starting():
quest1 = QtGui.QWidget()
quest1.setWindowTitle('New')
quest1.resize(900, 600)
quest1.show()
quest1.exec()
testing = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setWindowTitle('Title')
window.resize(900, 600)
MainText = QtGui.QLabel('<p align="center"; style="font-size: xx-large">Text</p>')
Mainbox = QtGui.QVBoxLayout()
# buttons
start = QtGui.QPushButton('Start')
quit = QtGui.QPushButton('Exit')
start.setFixedSize(70, 40)
quit.setFixedSize(70, 40)
buttons = QtGui.QHBoxLayout()
buttons.addWidget(start)
buttons.addWidget(quit)
# /buttons
Mainbox.addWidget(MainText)
Mainbox.addLayout(buttons)
window.setLayout(Mainbox)
QtCore.QObject.connect(quit, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT("quit()"))
QtCore.QObject.connect(start, QtCore.SIGNAL('clicked()'), starting)
window.show()
sys.exit(testing.exec_())
I think, I do wrong, is not required to make new window, but I don`t know what I must doing.

Qt UIs are built from widgets. Many widgets can have children. If you add/remove children, the UI will update accordingly.
The problem with the code above is that you don't add the new widget to a parent. So what happens is: You create the widget, you force it to appear, the function ends, the local variables (newwindow) end up on the trash and Python cleans the trash -> the widget is deleted again.
For the window to stay, you need to add it to some parent widget (probably the window). If you want to replace existing widgets, you need to remove them yourself.

Related

How to show message in popup window while python script is running [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Is there any way to show the message in popup window that process is running when python script start and disappear the popup window when process is done. Thanks in advance
A very flexible method you could look into is creating a tkinter window:
https://docs.python.org/3/library/tkinter.html
import tkinter as tk
import time
def some_function():
print('Do stuff')
time.sleep(3)
if __name__ == '__main__':
# Create window
window = tk.Tk()
# Create label
label_var = tk.StringVar()
label_var.set('Program is running...')
label = tk.Label(window, textvariable=label_var)
label.pack()
# Update and show window once
window.update_idletasks()
window.update()
# Your function code
some_function()
# Get rid of window
window.destroy()
Edit: just saw someone answered with tkinter in the comments... will leave this here as an example

closing dialog box also closes main window [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I created a dialog box & connected it to main window.Dialog purpose is to assign values to variables when buttons are pressed.My problem is that when i try to close dialog box it also closes the main window.This is code from main window file.
from dialog import Ui_Dialog as Form
This following is the function used to open dialog box
def open_dialog(self):
ap = QtWidgets.QApplication(sys.argv)
ap.setStyle('fusion')
dialog = QtWidgets.QDialog()
dialog.ui = Form()
dialog.ui.setupUi(dialog)
dialog.exec_()
I tried to fix it with help of google.I added (accept/reject) buttonbox of pyqt5 but that also close the main window with dialog box
Do not create new QApplication object in your code.Your code should be like this
def open_dialog(self):
dialog = QtWidgets.QDialog()
dialog.ui = Form()
dialog.ui.setupUi(dialog)
dialog.exec_()
remove this from code
ap = QtWidgets.QApplication(sys.argv)

Label in python tkinter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#writing introduction text
mn=tk.Label(window,text="WELCOME TO \n AAROGYA SETU \n SELF EXAMINATION FRONT",font=("Bookman old style",30,'bold','italic')).pack()
#linking sign up form
sig=tk.Label(window,text="click button to sign up",font=("Bookman old style",20,"bold")).pack()
Error message:
'Nonetype' has no attribute .grid()
I am unable to position my label (that I created using Tkinter) in my program. it doesn't take .grid() or .place() either.
What can be the possible reasons? my code execution otherwise is perfect.
You do the most common mistake
variable = Widget().pack()
which assign None to variable because pack()/grid()/place() returns None.
You have to do it in two steps:
variable = Widget()
variable.pack()
But it seems you may create other problem.
pack()/grid()/place() are different layout managers and you shouldn't use two layout managers on one widget.
So use pack() OR grid() OR place().
You don't need pack() if you want to use grid() or place().
variable = Widget()
variable.grid()
or
variable = Widget()
variable.place(...)
or
variable = Widget()
variable.pack()
from tkinter import *
main = Tk()
name = Label(main,text="WELCOME TO \n AAROGYA SETU \n SELF EXAMINATION FRONT",font=("Bookman old style",30,'bold','italic'),bg="light blue").pack()
#linking sign up form
sign=Label(main,text="click button to sign up",font=("Bookman old style",20,"bold"),bg="light blue").pack()
main.mainloop()
this should work

How can I create a text file that saves an entry from tkinter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is my current db.py file, it contains the tkinter code for creating the GUI:
import tkinter
import db
app = Tk()
app.geometry("450x300")
app.mainloop()
You can use a Entry widget with a linked variable to get the input from user. The content can then be retrieved from the variable and written to a file using file objects.
I like to use themed-tkinter(ttk). If you just starting with creating GUI, I suggest you read more about themed-tkinter here.
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
# StringVar has all the logic to store and update string values
content = tk.StringVar()
def callback():
# This function is invoked when button `submit` is clicked
with open('content.txt', 'w') as file:
file.write(content.get())
entry = ttk.Entry(root, textvariable=content).grid()
submit = ttk.Button(root, text='submit', command=callback).grid()
root.mainloop()
Edit: I worded my answer wrongly for which I appologize. Tkinter by itself is indeed robust and powerful. I found it more easier to use ttk in many cases and had a softcorner towards it.

QListWidget synchronized using common datasource [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a main window with a mdiArea in it. In the area there are 2 subwindows, and possibly more. I want to have everyone have a QListWidget that shares the information and they are synchronized. I searched but I can not find any help.
I was thinking of something like a static list, I want to share the data between all the sub-windows and allow anyone to work on them, 1 window at a time
A widget can only have one parent and it was drawn in the window that belongs to the parent, so the answer to your direct question is that it can not. But I think that in the background you want several views to share the same data and be synchronized, if so, the solution is to use a proxy so that several models share the same data at all times.
In the case of the QListWidget you can not set a model but it will be the one used as a base and the widgets copies will be QListView.
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
mdiarea = QtWidgets.QMdiArea()
self.setCentralWidget(mdiarea)
list_widget = QtWidgets.QListWidget()
for i in range(10):
it = QtWidgets.QListWidgetItem('item {}'.format(i))
it.setFlags(it.flags() | QtCore.Qt.ItemIsEditable)
list_widget.addItem(it)
sub_window = QtWidgets.QMdiSubWindow()
sub_window.setWidget(list_widget)
mdiarea.addSubWindow(sub_window)
for _ in range(4):
list_view = self.create_qlistview(list_widget.model())
sub_window = QtWidgets.QMdiSubWindow()
sub_window.setWidget(list_view)
mdiarea.addSubWindow(sub_window)
def create_qlistview(self, model):
proxy = QtCore.QIdentityProxyModel()
proxy.setSourceModel(model)
list_view = QtWidgets.QListView()
list_view.setModel(proxy)
return list_view
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())

Categories

Resources