I am using a calendar widget for Python. And I need to call the widget when a button is clicked.
The situation is that I cannot find what is the method in the calendar class that displays the widget itself.
The calendar class was taken from here:
http://www.eurion.net/python-snippets/snippet/Calendar_Date%20picker.html
Here are my imports:
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import time
import requests #needs to be installed
import pymysql #needs to be installed
import csv
import win32com.client #needs to be installed
from calendar import Calendar
import datetime
Here is the button creation:
# Calendar Buttons
calBut=ttk.Button(f2, width=4, text="Cal", command=Calendar.what_method?).grid(column=3,row=1, sticky=W)
As far as I know, I can just set the command of the button to call the widget display method located in the calendar class.
How to get the method that displays the calendar widget each time my button is clicked? None of the ones showing are displaying the widget.
Using Python 3.3.5
Spider
WinPython 3.3.5
**EDIT**
The program has tabs and the f2 indicates the tab where the button will be.
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import time
import requests #needs to be installed
import pymysql #needs to be installed
import csv
import win32com.client #needs to be installed
import datetime
from calendar import Calendar
import calendar
#################################
# Create Button Click Calendar
def callback():
root2=Toplevel(f2)
ttkcal = Calendar(root2,firstweekday=calendar.SUNDAY)
ttkcal.pack(expand=1, fill='both')
root2.update()
root2.minsize(root2.winfo_reqwidth(), root2.winfo_reqheight())
# Calendar Buttons
b=ttk.Button(f2, width=4, text="Cal", command=callback).grid(column=3,row=1, sticky=W)
When I press the button, it opens the calendar window, but it is empty. And the console gives me error:
TypeError: __init__() got multiple values for argument 'firstweekday
Thank you
Not so easy. The problem is that you mix the two GUI libraries. Therefore it is necessary two main event loops (at least): one for Tkinter code and one for PyQt code.
One way to do what you want - using subprocess and threading modules to run calendar.py in different thread. Example:
from tkinter import *
from tkinter import ttk
import subprocess
import threading
master = Tk()
def callback():
subprocess.call('python calendar.py')
b=ttk.Button(master, width=4, text="Cal", command=lambda:threading.Thread(target=callback).start()).grid(column=3,row=1, sticky=W)
mainloop()
Another way - creating Qt main event loop inside callback function (dirty solution):
from tkinter import *
from tkinter import ttk
from calendar import Calendar
import sys
from PyQt4 import QtGui
master = Tk()
def callback():
app = QtGui.QApplication(sys.argv)
gui = Calendar()
gui.show()
app.exec_()
b=ttk.Button(master, width=4, text="Cal", command=callback).grid(column=3,row=1, sticky=W)
mainloop()
EDIT: How to call widget.
First of all, look at this answer, and modify your ttkcalendar.py as kalgasnik suggested. Then try this:
from tkinter import *
from tkinter import ttk
from ttkcalendar import Calendar
import calendar
master = Tk()
def callback():
root2=Toplevel(master)
ttkcal = Calendar(root2,firstweekday=calendar.SUNDAY)
ttkcal.pack(expand=1, fill='both')
root2.update()
root2.minsize(root2.winfo_reqwidth(), root2.winfo_reqheight())
b=ttk.Button(master, width=4, text="Cal", command=callback).grid(column=3,row=1, sticky=W)
mainloop()
EDIT 2. Solving the problems
Ok, it seems I found all problems.
Actually, you import twice the same module - standard calendar module:
from calendar import Calendar
import calendar
But you do not import the class Calendar from ttkcalendar module (Do not forget to change it as described
here).
So, import should look like this:
import ttkcalendar
import calendar
Creating calendar (I changed the code a bit for clarity):
ttkcal = ttkcalendar.Calendar(root2,firstweekday=calendar.SUNDAY)
In your code, the main window is initialized twice:
line 15: master = Tk()
line 960: root = Tk()
You need to remove the first initialization.
You mix pack() and grid() in the same master window. According the docs, it is a bad idea:
Warning: Never mix grid and pack in the same master window. Tkinter
will happily spend the rest of your lifetime trying to negotiate a
solution that both managers are happy with. Instead of waiting, kill
the application, and take another look at your code. A common mistake
is to use the wrong parent for some of the widgets.
So, instead nb.pack(fill='both', expand='yes') you have to write something like this
nb.grid(column=0, row=0, sticky=(W, E))
Finally, here are links to the fixed code:
ttkcalendar.py (already modified, ready to use): https://gist.github.com/anonymous/5e0d973f57e185572df2
Your script with described modifications:
https://gist.github.com/anonymous/65cb808dc64e414c0c12
Related
I am trying a "Hello World" test on a GUI window, imported from tkinter. The problem starts when I try to run it and an error pops up in the terminal of VScode:
cannot import name 'geometry' from 'tkinter'.
import csv
from fileinput import filename
import os
from tkinter import *
from tkinter import ttk
from tkinter import Tk, Button, Frame, Entry, END
from tkinter import geometry
class attendance_tester(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
root = Tk()
root.geometry("800x800")
frm = ttk.Frame(root, padding=10)
frm.grid()
ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)
app = attendance_tester(master=root)
app.master.title("Student Attendance Program")
app.mainloop()
root.destroy()
from attendance_create import (func_create, func_edit_add,
func_ask_edit, func_edit_change, func_edit_sub,
func_edit_view, func_open, func_first_change)
with open(r"C:\\Users\\user_name\\Desktop\\attendance_sheet.csv") as file:
csv_file_read = csv.reader(file)
for row in csv_file_read:
print(row)
func_open()
attend_test = attendance_tester(root)
root.mainloop()
I have tried seemingly every solution I could find on the internet. I really need some help.
You don't need to import geometry to use it. Just remove the import line and it should work.
you shouldnt import it from the first place you just need to import the tkinter itself just remove that line and it should be just fine
There is no reason to import geometry, it works without that import line.
Also, you have a few duplicate imports which may become confusing. It's better to stick to fewer imports:
from tkinter import *
from tkinter import ttk
from tkinter import Tk, Button, Frame, Entry, END
I am trying to use the Azure theme in a tkinter application i made. I used the code from github to import the theme(https://github.com/rdbende/Azure-ttk-theme). I put the "azure.tcl" file in the folder with the python file and got the error "_tkinter.TclError: couldn't read file "./theme/light.tcl": no such file or directory". I then tried making a simpler tkinter program with the same ttk theme import code from before but this time in it's own folder with the tcl file. I still got the same error message. When i download the file from github, i ran the example python file and worked just fine.
Here is the code i use to import the file.
import tkinter as tk
from tkinter import Label, ttk
root = tk.Tk()
root.tk.call("source", "azure.tcl")
root.tk.call("set_theme", "dark")
root.geometry("300x300")
L1 = Label(root, text="Hello world")
L1.grid(row=1, column=1)
root.mainloop()
If you do not have this "TKinterModernThemes" library, then you should also install it with below code:
pip install TKinterModernThemes
Then, you can keep your script as it is but just a few arrangements.
Importing Library:
import TKinterModernThemes as TKMT
Threat like window.root is your tk.Tk() object:
import tkinter as tk
from tkinter import Label, ttk
import TKinterModernThemes as TKMT
window = TKMT.ThemedTKinterFrame("%yourProjectName","azure","dark")
window.root.geometry("300x300")
L1 = Label(window.root, text="Hello world")
L1.grid(row=1, column=1)
window.root.mainloop()
I want to print the condition weather on tkinter gui interface.So I wrote this code....
import tkinter as tk
from tkinter import *
from weather import Weather,Unit
win=tk.Tk()
weather = Weather(unit=Unit.CELSIUS)
location=weather.lookup_by_location('Dhaka')
condition=location.condition
label=Label(text=condition)
label.pack()
win.mainloop()
But the output sample is...
But I want output that show condition like Sunny,Thunderstorm etc.
You've linked the object to a label, you need to call the text. condition.text
import tkinter as tk
from tkinter import *
from weather import Weather,Unit
win=tk.Tk()
weather = Weather(unit=Unit.CELSIUS)
location=weather.lookup_by_location('Dhaka')
condition=location.condition
label=Label(text=condition.text)
label.pack()
win.mainloop()
I'm really stuck on a basilary things: I have this code
from tkinter import *
import sys
import subprocess
import tkinter as tk
def cd():
f=(subprocess.check_output("net view"))
e=(f.decode(sys.stdout.encoding))
label1=Label(text=e).pack()
def mainscreen():
mainscreen=Tk()
mainscreen.title("Terfysgol's kit V 2.0")
frame1=Frame(mainscreen)
frame1.pack()
puls1=Button(frame1,text="List of device", borderwidth= "2",command= cd).pack()
mainscreen()
When I run it all the time that I press the button it create a new label but I only want to update the text of the label1.
This is what you are after:
def cd():
f=(subprocess.check_output("net view"))
e=(f.decode(sys.stdout.encoding))
label1.config(text = e)
and then at the top of your program after your imports you need to put:
label1 = Label()
label1.pack()
Please note that I'm not suggesting this is good program structure, but that is up to you to sort out. This answer is just a quick fix to provide you with enough information to work out the rest of what you need.
Also you can remove the import tkinter as tk line already imports tkinter.
I'm just beginning to learn tkinter at the moment, and when importing messagebox I found that I must not really understand import statements.
The thing that confuses me is that:
import tkinter as tk
def text_box():
if tk.messagebox.askokcancel("Quit", "Never Mind"):
root.destroy()
root = tk.Tk()
button = tk.Button(root, text="Press the button", command=text_box)
button.pack()
root.mainloop()
compiles fine, but pressing the button gives the error 'module' object has no attribute 'messagebox', while the code:
import tkinter as tk
from tkinter import messagebox
...
if messagebox.askokcancel("Quit", "Never Mind"):
...
...works without a hitch.
I get a similar error if I import with from tkinter import *.
The help for tkinter shows messagebox in the list of PACKAGE CONTENTS, but I just can't load it in the normal way.
So my question is, why...and what is it about importing that I don't understand?
Just thought I should mention—the code only works in Python 3, and in Python 2.x messagebox is called tkMessageBox and is not defined in tkinter.
tkinter.messagebox is a module, not a class.
As it isn't imported in tkinter.__init__.py, you explicitly have to import it before you can use it.
import tkinter
tkinter.messagebox # would raise an ImportError
from tkinter import messagebox
tkinter.messagebox # now it's available eiter as `messagebox` or `tkinter.messagebox`
try this
import sys
from tkinter import *
... and your code