How to make new label when the program is running in tkinter? - python-3.x

I want to make a new label when the program is running when the button is pressed. I am working in Tkinter python v3.
Here is my code:
from tkinter import *
from tkinter import ttk
import tkinter as tk
import copy
class App(ttk.Frame):
def __init__(self, master):
self.newwindow = master
self.pocetnik_label = Label(master, text = 'Pocetnik')
self.pocetnik_label.pack(side = LEFT)
self.dodaj_button = Button(master, text = '+', command = self.pocetnik)
self.dodaj_button.pack(side = RIGHT)
self.newwindow.mainloop()
def pocetnik(self):
b2= tk.Toplevel(self.newwindow)
self.ime_label = Label(b2, text = 'Ime').grid(row = 0, column = 0)
self.ime_entry = Entry(b2, bd = 5).grid(row = 0, column = 1)
self.vreme_label = Label(b2, text = 'Vreme').grid(row = 1, column = 0)
self.vreme_entry = Entry(b2, bd = 5).grid(row = 1, column = 1)
self.napravi_button = Button(b2, text = 'Napravi').grid(row = 3, column = 0)
master = Tk()
pocetnik = App(master)

I've checked your code on my computer and it kind of works :-)
If I press the '+' button a new window appears.
from tkinter import ttk
import tkinter as tk
import copy
class App(ttk.Frame):
def __init__(self, master):
self.newwindow = master
self.pocetnik_label = Label(master, text='Pocetnik')
self.pocetnik_label.pack(side=LEFT)
self.dodaj_button = Button(master, text='+', command=self.pocetnik)
self.dodaj_button.pack(side=RIGHT)
self.newwindow.mainloop()
def pocetnik(self):
b2 = tk.Toplevel(self.newwindow)
self.ime_label = Label(b2, text='Ime').grid(row=0, column=0)
self.ime_entry = Entry(b2, bd=5).grid(row=0, column=1)
self.vreme_label = Label(b2, text='Vreme').grid(row=1, column=0)
self.vreme_entry = Entry(b2, bd=5).grid(row=1, column=1)
self.napravi_button = Button(b2, text='Napravi').grid(row=3, column=0)
master = Tk()
pocetnik = App(master)
Maybe you want to become this new window a modal dialoge? You could have a look at How to create a modal dialog in tkinter?

Related

Why does my Tkinter window crash when I try to display a label?

I am using Pycharm and Python 3.8.
My Tkinter window stops responding and causes my program to crash.
Pycharm presents the following line in its output: Process finished with exit code -805306369 (0xCFFFFFFF)
I have the following code for displaying the progress of a for loop:
import tkinter as tk
from tkinter import filedialog, ttk
from tkinter.ttk import Progressbar
import json
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.filename = ''
tk.Button(self, text = 'Upload File', command = self.get_file).grid(row = 0, column = 0)
tk.Button(self, text = 'Verify', command = self.verify).grid(row = 1, column = 0)
# tk.Label(self, text = 'some name').grid(row = 0, column = 1)
self.mainloop()
def get_file(self):
self.filename = tk.filedialog.askopenfilename(title = "Select File", filetypes = [("JSON", "*.json")])
tk.Label(self, text = self.filename).grid(row = 0, column = 1)
def verify():
lbl = tk.Label(self, text = 'Verifying Trustee Public Keys')
lbl.grid(row = 2, column = 0)
prog1 = ttk.Progressbar(self,
length = 100,
orient = "horizontal",
maximum = len(self.data["trustee_public_keys"]),
value = 0)
prog1.grid(row = 3, column = 0)
i = 1
for trustee_key_list in self.data['trustee_public_keys']:
print('Trustee :', i)
//some code
i = i+1
prog1['value'] = i
The get_file part works. But, when I click the verify button to execute the verify() function. The entire window freezes and I get a not responding message.
WHat is the problem with the following part of my code?
lbl = tk.Label(self, text = 'Verifying Trustee Public Keys')
lbl.grid(row = 2, column = 0)
What am I doing wrong?

python 3+ with tkinter: modal window not appearing

I'm new to python. I'm trying to set up a GUI with a main window and a modal window to receive the database credentials. The main window has to be in the background when the modal window appears and the user should not interact with it. Actually the application should terminate in case the user fails to provide a valid username and password. I'm presenting part of the code here. What I get is the main window (the modal window does not appear at all) and I can't do anything but shut the program down - not directly; only through visual studio.
'''
import tkinter as tk
class login:
def checkDbCredentials(self, user, password):
if (len(user)<4 or len(password)<4):
return 0
return 1
def ok(self):
us = self.user.get()
pw = self.password.get()
if self.checkDbCredentials(us, pw)==0:
tk.messagebox.showinfo("Error", "Username and Password do not match")
sys.exit(0)
self.dic['user']=us
self.dic['pass']=pw
self.win2.destroy()
def cancel(self, event=None):
self.parent.focus_set()
self.destroy()
def widgets(self):
x = self.win2.winfo_screenwidth()
y = self.win2.winfo_screenheight()
xx = int((x-270)/2)
yy = int((y-85)/2)
geo = "270x85+" + str(xx) + "+" + str(yy)
self.win2.geometry(newGeometry=geo)
self.tempLab= tk.Label(self.win2, text="Username:", pady=5)
self.tempLab1 = tk.Label(self.win2, text="Password:")
self.iuser = tk.Entry(self.win2, textvariable = self.user, width=30)
self.ipass = tk.Entry(self.win2, textvariable = self.password, width=30, show="*")
self.tempLab.grid(column=0, row=0)
self.tempLab1.grid(column=0, row=1)
self.iuser.grid(column=1, row=0)
self.ipass.grid(column=1, row=1)
self.bt = tk.Button(self.win2, text="Submit", command=lambda: self.ok())
self.bt.grid(column=0, row=2, columnspan=2, pady=5)
self.win2.bind("<Return>", self.ok)
self.win2.bind("<Escape>", self.cancel)
def __init__(self, dic, parent):
self.win2 = tk.Toplevel(parent)
self.parent=parent
self.dic = dic
self.user = tk.StringVar()
self.password = tk.StringVar()
self.win2.overrideredirect(1)
self.widgets()
self.win2.transient(parent)
self.win2.grab_set()
self.win2.protocol("WM_DELETE_WINDOW", self.cancel)
self.parent.wait_window(self.win2)
And the main class code is the following:
import tkinter as tk
from tkinter import ttk, X, Y, BOTH
from tkinter import messagebox
import sys
import login
class mainWindow:
def connect(bt, fr):
notImplementedYet()
def notImplementedYet():
msg = tk.messagebox
msg.showinfo("Warning", "Not Implemented yet!")
btX = 20
btY = 5
btSunken = '#aec2c2' # sunken button color
btRaised = '#85adad' # normal button color
rw = 0 # starting row for buttons
cl = 0 # starting column
dbCredentials = {"user":"", "pass":""}
win = tk.Tk()
win.title("Title")
win.geometry(newGeometry="1366x700+00+00")
win.iconbitmap(bitmap="bitmap.ico")
fr1 = tk.Frame(win, width=1366, height=50, background='beige')
fr1.grid(column=cl, row=rw, rowspan=5)
fr2 = tk.Frame(win)
fr2.grid(column=0, row=1)
lb2 = tk.Label(fr2, text="frame 2", background=btRaised)
btConnect = tk.Button(fr1, text="Connect", background = btRaised ,command=lambda: connect(btConnect, fr1), width=btX, height=btY)
btConnect.grid(column=0, row=0)
log = login.login(dbCredentials, win)
win.wait_window(log.win2)
print(dbCredentials)
win.mainloop()
win = mainWindow()
The question is why is the modal window not appearing? I've tried placing the 'wait_window' in the other class as well but it's still not working.

I want to print my all excel data in new tkinter window using python

I would like print all the information present in the excel data in new tkinter window
You can use Tkinter's grid.
For example, to create a simple excel-like table:
from Tkinter import *
root = Tk()
height = 5
width = 5
for i in range(height): #Rows
for j in range(width): #Columns
b = Entry(root, text="")
b.grid(row=i, column=j)
mainloop()
To print, consider the following example in which I make a button with Tkinter that gets some text from a widget and then prints it to console using the print() function.
from tkinter import *
from tkinter import ttk
def print_text(*args):
try:
print(text1.get())
except ValueError:
pass
root = Tk()
root.title("Little tkinter app for printing")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N,W,E,S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
text1 = StringVar()
text_entry = ttk.Entry(mainframe, width = 20, textvariable=text1)
text_entry.grid(column = 1, row = 2, sticky = (N,W,E,S))
ttk.Button(mainframe, text = "Print!", command =
print_text(text1)).grid(column = 1, row = 3, sticky = (E))
for child in mainframe.winfo_children():
child.grid_configure(padx = 5, pady = 5)
text_entry.focus()
root.bind('<Return>', print_text)
root.mainloop()

How to get user to select specific date from Calendar, Python?

Well I have written a program in Python which displays A calendar. However what I want to do is, get the user to select a date from the calendar which I will then print.
Request some guidance as to how can I accomplish the same
Code snippet as follows:
import calendar
from tkinter import *
gui = Tk()
gui.title("Calendar")
def cal():
y = e1.get()
m = e2.get()
cal_x = calendar.month(int(y),int(m),w = 2, l = 1)
print (cal_x)
cal_out = Label(gui, text=cal_x, font=('courier', 12, 'bold'), bg='lightblue')
cal_out.pack(padx=3, pady=10)
label1 = Label(gui, text="Year:")
label1.pack()
e1 = Entry(gui)
e1.pack()
label2 = Label(gui, text="Month:")
label2.pack()
e2 = Entry(gui)
e2.pack()
button = Button(gui, text="Show",command=cal)
button.pack()
gui.mainloop()

Error: Attribute error in TCL

I am trying to create an application in Python GUI using tkinter. Here is the code I'm using for the GUI part. Whenever I try to access my Entry Widget I get error.
Ex:Sin_put.get() should give me the text in the Entry widget but it gives me an error
AttributeError: 'NoneType' object has no attribute 'get'
I'm relatively new to Python. So if you guys have any suggestions to improve the functionality you're most welcome.
from tkinter import *
from tkinter import ttk
sys.path.insert(0, 'home/ashwin/')
import portscanner
class App:
def __init__(self, master):
master.option_add('*tearOff', False)
master.resizable(False,False)
self.nb = ttk.Notebook(master)
self.nb.pack()
self.nb.config(width=720,height=480)
self.zipframe = ttk.Frame(self.nb)
self.scanframe = ttk.Frame(self.nb)
self.botframe = ttk.Frame(self.nb)
self.mb = Menu(master)
master.config(menu = self.mb)
file = Menu(self.mb)
info = Menu(self.mb)
self.mb.add_cascade(menu = file, label = 'Tools')
self.mb.add_cascade(menu = info, label = 'Help')
file.add_command(label='Zip Cracker', command = self.zipframe_create)
file.add_command(label='Port Scanner', command = self.scanframe_create)
file.add_command(label='Bot net', command =self.botframe_create)
info.add_command(label='Usage', command=(lambda:print('Usage')))
info.add_command(label='About', command=(lambda:print('About')))
def zipframe_create(self):
self.nb.add(self.zipframe,text='Zip')
self.zipframe.config(height=480,width=720)
zlabel1 = ttk.Label(self.zipframe, text='Select the zip file').grid(row=0,column=0, padx=5, pady=10)
zlabel2 = ttk.Label(self.zipframe, text='Select the dictionary file').grid(row=2,column=0, padx=5)
ztext1 = ttk.Entry(self.zipframe, width = 50).grid(row=0,column=1,padx=5,pady=10)
ztext2 = ttk.Entry(self.zipframe, width = 50).grid(row=2,column=1,padx=5)
zoutput = Text(self.zipframe, width=80, height=20).grid(row=3,column=0,columnspan = 3,padx=5,pady=10)
zb1 = ttk.Button(self.zipframe, text='Crack', width=10).grid(row=0,column=2,padx=5,pady=10)
def scanframe_create(self):
self.nb.add(self.scanframe,text='Scan')
self.scanframe.config(height=480,width=720)
slabel1 = ttk.Label(self.scanframe, text='IP address').grid(row=0,column=0, padx=5, pady=10)
sin_put = ttk.Entry(self.scanframe, width = 50).grid(row=0,column=1,padx=5,pady=10)
soutput = Text(self.scanframe, width=80, height=20).grid(row=3,column=0,columnspan = 3,padx=5,pady=10)
sb1 = ttk.Button(self.scanframe, text='Scan', width=6,command= print('Content: {}'.format(sin_put.get()))).grid(row=0,column=2,padx=5,pady=10)
def botframe_create(self):
self.nb.add(self.botframe,text='Bot')
self.botframe.config(height=480,width=720)
blabel1 = ttk.Label(self.botframe, text='IP address').grid(row=0,column=0, padx=5, pady=10)
blabel2 = ttk.Label(self.botframe, text='Username').grid(row=1,column=0, padx=2)
blabel3 = ttk.Label(self.botframe, text='password').grid(row=2,column=0, padx=2)
btext1 = ttk.Entry(self.botframe, width = 30).grid(row=0,column=1,padx=5,pady=10)
btext2 = ttk.Entry(self.botframe, width = 30).grid(row=1,column=1)
btext2 = ttk.Entry(self.botframe, width = 30).grid(row=2,column=1)
boutput = Text(self.botframe, width=80, height=20).grid(row=3,column=0,columnspan = 3,padx=5,pady=10)
bb1 = ttk.Button(self.botframe, text='Connect', width=8).grid(row=2,column=2,padx=5,pady=10)
def main():
root = Tk()
feedback = App(root)
root.mainloop()
if __name__ == "__main__": main()
grid() returns None so sin_put will always equal None. Instead of passing the Tkinter ID to grid() you have to store it first if you want to reference it later. Note that for the Buttons, putting it all on one line is fine as you don't use the button's ID later.
sin_put=ttk.Entry(self.scanframe, width = 50) ## stores the return ID from Entry
sin_put.grid(row=0,column=1,padx=5,pady=10) ## don't catch return from grid as it is None

Resources