How to get user to select specific date from Calendar, Python? - python-3.x

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()

Related

Separate fuction for each button

Trying to create a python program in tkinter to mark attendance of persons engaged in a special duty. By clicking the button containing their name the figure right to their name is incremented by 1. While i created the function when any button is placed all figures are got incremented. And the attempt finally reached here.
from tkinter import *
staff=['GEORGE ', 'JAMES ', 'THOMAS', 'MATHEW',
'CLETUSCRUZ', 'FREDY', 'PAUL', 'MEGANI', 'BILL',
'JULIA ']
def factory(number):
def f():
number.set(number.get()+1)
return f
functions =[]
for i in range(10):
functions.append(factory(i))
for i in functions:
f()
window = Tk()
window.title(" Duty List")
window.geometry('320x900')
number = IntVar()
row_value =3
for i in staff:
ibutton = Button(window, text= i, command=clicked)
ibutton.grid(column=1, row=row_value)
ilabel = Label(window, textvariable=number)
ilabel.grid(column=2,row=row_value)
row_value+=1
window.mainloop()`
Factory now creates a unique IntVar for each individual.
link connects button press to onclick for processing IntVar
I've used columnconfigure to push the numbers to the right hand side of window
import tkinter as tk
staff=["GEORGE ", "JAMES ", "THOMAS", "MATHEW",
"CLETUSCRUZ", "FREDY", "PAUL", "MEGANI", "BILL",
"JULIA "]
window = tk.Tk()
window.title(" Duty List")
window.columnconfigure(1, weight = 1)
def factory(n):
return tk.IntVar(value = n)
def onclick(a):
b = a.get()+1
a.set(b)
def link(a, b):
return lambda: a(b)
for i,n in enumerate(staff):
b = factory(0)
a = tk.Button(window, text= n, bd = 1)
a.grid(column = 0, row = i, sticky = tk.NW, padx = 4, pady = 4)
a["command"] = link(onclick, b)
l = tk.Label(window, text = "", textvariable = b, anchor = tk.NW)
l.grid(column = 1, row = i, sticky = tk.NE, padx = 4, pady = 4)
window.geometry("200x319")
window.resizable(False, False)
window.mainloop()

How to make new label when the program is running in tkinter?

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?

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()

Code using Tkinter works in IDLE but not in terminal

This is (part of) my very first program. It uses Tkinter to output text in a scrolled textbox. I used Python 3.6.4 and IDLE and it works perfectly, but when I run it from terminal/Atom once I click ok after selecting the options from the dropdown menus it just closes without errors, while in IDLE it correctly outputs all the text in the textbox.
I want to use Py2app to make a standalone, but for this the code needs to execute properly from terminal.
Here are the main snippets from the code. I'm just coding for a few months so any detailed help would be much appreciated.
from tkinter import *
from collections import OrderedDict
from tkinter.scrolledtext import *
from collections import Counter
master = Tk()
master.title("App")
master.geometry("600x665")
master.lift()
master.attributes('-topmost', True)
mvar = IntVar()
mvar1 = IntVar()
var = StringVar(master)
var.set("Asc")
var1 = StringVar(master)
var1.set("Ar")
x = OptionMenu(master, var, "Ar", "Ta", "Ge","Can","Le","Vi","Li","Sc","Sa","Cap","Aq","Pi")
x.grid(column =2,row =1)
x1 = OptionMenu(master, var1, "Ar", "Ta", "Ge","Can","Le","Vi","Li","Sc","Sa","Cap","Aq","Pi")
x1.grid(column =2,row =2)
def redirector(inputStr):
txt.insert(INSERT, inputStr)
sys.stdout.write = redirector
def ok():
redirector("Thanks for using the app")
master.quit()
label1 = Label(text=" Welcome to the app",bg="#C2DFFF",font=("Times New Roman",18))
label1.grid(column=0,row=0)
label2 = Label(text="Ma: ",bg="#C2DFFF")
label2.grid(column=0,row=2)
txt = ScrolledText(master, bg="#C2DFFF", width = 97, height= 25, font = "Arial 11")
txt.grid(column = 0, row = 14, columnspan=3)
button = Button(master, text="OK", default ='active',command=ok).grid(column=2,row=11)
button = Button(master, text="Reset", default ='active',command=reset).grid(column=2,row=12)
button = Button(master, text ="Cancel",command = cancel).grid(column=0,row=11)
C1 = Checkbutton(master, state = ACTIVE, variable = mvar)
C1.grid(column = 1, row=2)
C2 = Checkbutton(master, state = ACTIVE, variable = mvar1)
C2.grid(column = 1, row=3)
master.mainloop()
This is how the GUI looks like
You cannot generally reassign sys.stdout.write - it is normally a read-only attribute of a built-in file object. The proper way to do output redirection is to assign a new object to sys.stdout, that has a write() method.
Your code works in IDLE only because IDLE has already replaced the built-in sys.stdout with its own redirection object, which is fully modifiable.

iterate through a list and get user response to each item using tkinter GUI

I am being particularly obtuse. I am iterating through a list of technical italian words and wanting to insert a translation using a tkinter interface. There is no problem doing this without the GUI: My problem is that I cannot figure out how to do an iteration, load a word into a ttk.Label and wait for a user entry in a ttk.Entry field. I have searched and found explanations, but I am at a loss how to apply the suggestions. This is my code using a trivial list of words:
from tkinter import ttk
import tkinter as tk
def formd():
list_of_terms = ['aardvark', 'ant','zombie', 'cat', 'dog', 'buffalo','eagle', 'owl','caterpiller', 'zebra', 'orchid','arum lily' ]
discard_list = []
temp_dict={}
list_of_terms.sort()
for item in list_of_terms:
listKey.set(item)
# need to wait and get user input
translation =dictValue.get()
temp_dict[item]=translation
discard_list.append(item)
# check if it has worked
for key, value in temp_dict.items():
print(key, value)
# GUI for dict from list
LARGE_FONT= ("Comic sans MS", 12)
root = tk.Tk()
root.title('Nautical Term Bilingual Dictionary')
ttk.Style().configure("mybtn.TButton", font = ('Comic sans MS','12'), padding = 1, foreground = 'DodgerBlue4')
ttk.Style().configure('red.TButton', foreground='red', padding=6, font=('Comic sans MS',' 10'))
ttk.Style().configure('action.TLabelframe', foreground = 'dodger blue3')
#.......contents frames.....................
nb = ttk.Notebook(root)
page5 = ttk.Frame(nb)
# declare variables
listKey= tk.StringVar()
dictValue = tk.StringVar()
# widgets
keyLabel =ttk.Label( page5, text = "key from list", font=LARGE_FONT).grid(row=3, column = 0)
Keyfromlist =ttk.Label(page5, width = 10, textvariable = listKey).grid(row = 3, column = 1)
valueLabel =ttk.Label( page5, text = "enter translation", font=LARGE_FONT).grid(row=3, column = 2)
listValue =ttk.Entry(page5, textvariable =dictValue, width = 15).grid(row = 3, column = 3)
#listValue.delete(0,'end')
#listValue.focus_set()
# add buttons
b1 = ttk.Button(page5, text="add to dictionary",style = "mybtn.TButton", command = formd)
b1.grid(row = 5, column = 0)
b5 = ttk.Button(page5, text="clear entry", style ="mybtn.TButton")
b5.grid(row = 5, column = 2)
nb.add(page5, text='From List')
nb.pack(expand=1, fill="both")
for child in root.winfo_children():
child.grid_configure(padx =5, pady=5)
if __name__ == "__main__":
root.mainloop()
I wonder whether someone could take the time to suggest a solution, please. How to stop a while loop to get input from user using Tkinter? was the one suggestion that I cannot figure how to use in my example
tkinter doesn't "play nice" with while loops.
Fortunately for you, you don't need to use one.
You can do something like the below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.list = ['aardvark', 'ant','zombie', 'cat', 'dog', 'buffalo','eagle', 'owl','caterpiller', 'zebra', 'orchid','arum lily' ]
self.text = Label(self.root, text="aardvark")
self.entry = Entry(self.root)
self.button = Button(self.root, text="Ok", command=self.command)
self.text.pack()
self.entry.pack()
self.button.pack()
def command(self):
print(self.text.cget("text")+" - "+self.entry.get())
try:
self.text.configure(text = self.list[self.list.index(self.text.cget("text"))+1])
except IndexError:
self.entry.destroy()
self.button.destroy()
self.text.configure(text = "You have completed the test")
root = Tk()
App(root)
root.mainloop()
This essentially uses the Button widget to iterate to the next text and get the next input.

Resources