Label in python tkinter [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 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

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

Why is tkinter <Enter> and <Leave> event not working? [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 1 year ago.
Improve this question
I am trying to get my widget to light up when the mouse is hovering over it. Here is what I have tried:
self.buttons = []
self.buttonTips = ["Select and Add a clip to the project", "Order the clips in your project", "Trim the selected clip",
"Cut out audio breaks in the selected clip", "Preview clip (in new window)", "Render/Export your video"]
self.commands = [self.getClipPath, self.reorderClips, self.trimClip, self.cutOutAudio, self._preview, self.finishVideo]
self.images = [_addImage, _switchImage, _trimImage, _autoTrimImage, _previewImage, _exportImage]
for index, tip in enumerate(self.buttonTips):
self.buttons.append(Button(self.root, image=self.images[index], command=self.commands[index], bg=_bgcolor, relief=FLAT))
self.buttons[index].photo = self.images[index]
self.buttons[index].place(x=index * 30, y=490)
self.buttons[index].bind("<Enter>", func=partial(changeButtonBG, 1, self.buttons[index]))
self.buttons[index].bind("<Leave>", func=partial(changeButtonBG, 0, self.buttons[index]))
addToolTip(self.buttons[index], tip)
When I change the event type to <Motion> the function runs perfectly, but when I use or it doesn't work? Anyone know why?
Ok I have figured out my problem. In this line here:
addToolTip(self.buttons[index], tip)
the addToolTip() function also adds a bind to the widget. So I edited the binds so they had:
button.bind(func=function, add="+")
which adding the add="+" made it 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.

tkinter display values in a popup [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 4 years ago.
Improve this question
I'm trying to make some basic GUI and I'm having some trouble with this code:
with open(project_dir + 'logs/wash.log') as f:
for line in f.readlines():
if not line.startswith('BSSID ') and \
not line.startswith('------------'):
print(line)
At this point of the application I have the tkinter root already opened in the background and a terminal opened too, I would like to open a new tkinter window in which display the line printed above, I suppose adding a label in the window for each line that I need to display.
I tried tk.Toplevel() but I don't know how to make the new window in which display the strings.
My problem is I am trying to create a new window and print the strings there, I have tried tk.Toplevel() but I don't know how to make the new window in which to display the strings
From your comment:
my problem is to create a new window and print there the strings, I tried tk.Toplevel() but I don't know how to make the new window in which display the strings
This is a simple example but should help.
I have a button on the root window that links to a function called new_window(). This function will create a top level window containing a text box widget. We then use the with open statement to write the data to the text box.
import tkinter as tk
root = tk.Tk()
def new_window():
top = tk.Toplevel(root)
my_text_box = tk.Text(top)
my_text_box.pack()
with open(project_dir + 'logs/wash.log') as f:
for line in f.readlines():
if not line.startswith('BSSID ') and \
not line.startswith('------------'):
my_text_box.insert("end", line)
open_new_window = tk.Button(root, text="Open Toplevel", command=new_window)
open_new_window.pack()
root.mainloop()

How to add new label in window? [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 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.

Categories

Resources