Python Tkinter Listing options not showing info - python

I have a very basic question, i.e. by using Python Tkinter window I want to show list box and from that list, I want to show some information which I select. But I am getting some error.
import Tkinter
from Tkinter import*
import tkMessageBox
window = Tk()
window.title('ex - 4,listing option')
frame = Frame(window)
listbox = Listbox(frame)
listbox.insert(1, 'Manual')
listbox.insert(2, 'Auto')
listbox.insert(3, 'AI')
def dialog():
tkMessageBox('selection','your chice:' + \
listbox.get(listbox.curselection()))
btn = Button(frame, text = 'Choose',command = dialog)
btn.pack(side = RIGHT, padx = 5)
listbox.pack(side = LEFT)
frame.pack(padx = 30, pady = 30)
window.mainloop()
The error is:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Users\Subhro Jyoti\Desktop\python\ex - 4,listing option", line 12, in dialog
tkMessageBox('selection','your chice:' + listbox.get(listbox.curselection()))
TypeError: 'module' object is not callable

tkMessageBox is a collection of different types of message boxes, you have to indicate which one you want to use. For example:
def dialog():
tkMessageBox.showinfo('selection','your chice:' +
listbox.get(listbox.curselection()))
Your choices are showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, or askretrycancel

Related

Tkinter: set column width and font size for all labels?

Just started work on some GUI. I am an absolutely new to tkinter.
I can set the label width and font size using .config for individual labels.
I would like to make this the default for a specific column.
I tried:
Label.config(width=70, font=('Courier',15))
But keep getting errors:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 8, in
Label.config(width=70, font=('Courier',15))
TypeError: configure() missing 1 required positional argument: 'self'
What is the correct way to do this?
Full Code:
from tkinter import *
from tkinter import ttk
reasons_window = Tk()
ttk.Style.configure('TLabel',width=70,font=('Courier',15))
reasons_window.geometry("500x200")
# Create rows for the reasons to be entered.
label1 = ttk.Label(reasons_window, text="Qty")
label1.grid(row=0,column=0)
# for field in fields
e1 = Entry(reasons_window)
e1.grid(row=0,column=1)
def eval_click():
if int(e1.get()) == 100:
print('GO AHEAD')
eval_button = Button(reasons_window, text="Evaluate", command=eval_click)
eval_button.grid(row=3, column=0)
reasons_window.mainloop()
You can do this with the ttk submodule. Adapted from the ttk docs:
from tkinter import ttk
import tkinter
root = tkinter.Tk()
ttk.Style().configure('TLabel', width=70, font=('Courier',15))
btn = ttk.Label(text="Sample")
btn.pack()
root.mainloop()

How to make an entry int in tkinter

i want to create a area of circle calculator because i have some free time so i tried to run it first in to the terminal and it works but when i decide to add a litte GUI well i got an error
My code is this
from tkinter import *
screen = Tk()
screen.title("Area of Circle Calculator")
screen.geometry("500x500")
def submit():
global done
pi = 3.14159265359
an = int(pi * radius*radius)
laod = Label(screen, text = "Hello" % (an))
load.grid()
ask = Label(screen, text = "What is the radius of the circle?")
ask.pack()
radius = Entry(screen)
radius.pack()
done = Button(screen, text="submit", command = submit)
done.pack()
screen.mainloop()
and this is the error that i got
C:\Users\Timothy\Desktop>python aoc.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Timothy\AppData\Local\Programs\Python\Python37-32\lib\tkinter\_
_init__.py", line 1705, in __call__
return self.func(*args)
File "aoc.py", line 8, in submit
an = int(pi * radius*radius)
TypeError: unsupported operand type(s) for *: 'float' and 'Entry'
C:\Users\Timothy\Desktop>
i try to add int() in to the entry but it dosent work
You need to call radius.get() to get the Entry widget's current value.
Here's some documentation on it.
There were some other errors and a typo in your code, so I fixed them and made it more PEP 8 - Style Guide for Python Code compliant.
Here's the result:
from math import pi
from tkinter import *
screen = Tk()
screen.title("Area of Circle Calculator")
screen.geometry("500x500")
def submit():
r = float(radius.get())
an = pi * r**2
load = Label(screen, text="Hello %f" % (an))
load.pack()
ask = Label(screen, text="What is the radius of the circle?")
ask.pack()
radius = Entry(screen)
radius.pack()
done = Button(screen, text="submit", command=submit)
done.pack()
screen.mainloop()

_tkinter.TclError: can't pack when trying to add ttkcalendar into tkinter GUI

I'm trying to add a ttk calendar into my Tkinter GUI. The problem is that it raises _tkinter.TclError: can't pack .34164128 inside .34161248.34161448.34161608
import Tkinter
import tkSimpleDialog
import ttkcalendar
class CalendarDialog(tkSimpleDialog.Dialog):
"""Dialog box that displays a calendar and returns the selected date"""
def body(self, master):
self.calendar = ttkcalendar.Calendar(master)
self.calendar.pack()
def apply(self):
self.result = self.calendar.selection
# Demo code:
def main():
root = Tkinter.Tk()
root.wm_title("CalendarDialog Demo")
def onclick():
print 'click'
cd = CalendarDialog(root)
button = Tkinter.Button(root, text="Click me to see a calendar!", command=onclick)
button.pack()
root.update()
root.mainloop()
if __name__ == "__main__":
main()
TRACEBACK:
File "C:/Users/Milano/PycharmProjects/MC/plots/ds.py", line 32, in <module>
main()
File "C:/Users/Milano/PycharmProjects/MC/plots/ds.py", line 23, in main
cd = CalendarDialog(root)
File "C:\Python27\lib\lib-tk\tkSimpleDialog.py", line 64, in __init__
self.initial_focus = self.body(body)
File "C:/Users/Milano/PycharmProjects/MC/plots/ds.py", line 9, in body
self.calendar = ttkcalendar.Calendar(master)
File "C:\Users\Milano\PycharmProjects\MC\plots\ttkcalendar.py", line 52, in __init__
self.__place_widgets() # pack/grid used widgets
File "C:\Users\Milano\PycharmProjects\MC\plots\ttkcalendar.py", line 110, in __place_widgets
self._calendar.pack(in_=self, expand=1, fill='both', side='bottom')
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1940, in pack_configure
+ self._options(cnf, kw))
_tkinter.TclError: can't pack .34164128 inside .34161248.34161448.34161608
Do you know where is the problem?
The fault is that you don't have an __init__ method in the class CalendarDialog. So just rename the body method to __init__. Now you have initialized the instance every time one is made and a pack() method is defined.
I also encountered this problem putting a ttkCalendar into a Dialog box.
I suspect the author of this post "borrowed" the same code for building a calendar as I did:
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=2ahUKEwiWgYWKsJ3nAhVKl3IEHYrhCU8QFjABegQICBAB&url=https%3A%2F%2Fsvn.python.org%2Fprojects%2Fpython%2Ftrunk%2FDemo%2Ftkinter%2Fttk%2Fttkcalendar.py&usg=AOvVaw0ifTox4EI7CtBFWlRYD_m9
There are two problems I found using this code to create a Calendar object and placing it into a Dialog box.
The first one causes the traceback as shown in the post. The fix is to modify the ttkcalendar.py file to pack the calendar when it is created, not after it is created using the pack() function.
Here is the diff:
102c102
< self._calendar = ttk.Treeview(show='', selectmode='none', height=7)
---
> self._calendar = ttk.Treeview(self, show='', selectmode='none', height=7)
109c109
< self._calendar.pack(in_=self, expand=1, fill='both', side='bottom')
---
> self._calendar.pack(expand=1, fill='both', side='bottom')
Once you make this change, the calendar will appear in the dialog box. However, your problems are not yet done. Another exception occurs when trying to set the minimum size of the calendar:
Exception in Tkinter callback
Traceback (most recent call last):
File "/home/richawil/Applications/anaconda3/envs/TLM/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/richawil/Documents/Programming/Apps/TLM/TLM/ttkcalendar.py", line 134, in __minsize
width, height = self._calendar.master.geometry().split('x')
AttributeError: 'Calendar' object has no attribute 'geometry'
I have not been able to fix this issue other than to comment out the call to self.__minsize.
63c62,63
< self._calendar.bind('<Map>', self.__minsize)
---
> # Commented out because _calendar object does not support geometry() function
> #self._calendar.bind('<Map>', self.__minsize)

Error button is not defined?

This is my code:
import sys
from tkinter import *
def next_screen(names):
for widget in names:
widget.place_forget()
def forget_page1():
widgets = [mLabel1, button]
next_screen(widgets)
mGui = Tk ()
mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = Label (text = "Welcome to MyMathDictionary. Press Next to continue.",
fg = "blue",bg = "white")
mLabel1.place (x= 150,y = 200)
button = Button (text = "Next", command = forget_page1 ())
button.place(x = 275,y = 230)
mGui.mainloop()
It tells me :
Traceback (most recent call last): File
"C:\Python33\Projects\MyMathDictionary.py", line 24, in <module>
button = Button (text = "Next", command = forget_page1 (mLabel,button)) NameError: name 'button' is not defined
What does this error message mean?
Change this line of code:
button = Button (text = "Next", command = forget_page1 ())
To this:
button = Button (text = "Next", command = forget_page1)
Your problem was that you were calling forget_page1 before the window loaded.
Also, as the comments have already said, your error is different than your code. But, I'll go over that quickly too just in case. If you want to send arguments to a button's command function, you need to use a lambda:
button = Button(command = lambda: func(arg1, arg2))

button1 is not defined?

This is my code :
import sys
from tkinter import *
#first new screen
def hypoténusegetdef ():
widgets1 = button1
nextscreen1(widgets1)
def next_screen1(names):
for widget in names:
widget.place_forget()
hyplabel1 = Label (text = "This is my text")
def next_screen(names):
for widget in names:
widget.place_forget()
button1 = Button (text = "Button1",fg = "blue",command = hypoténusegetdef)
button1.grid (row = 1,column = 2)
def forget_page1():
widgets = [mLabel1, button]
next_screen(widgets)
################################################################################
#first page things
mGui = Tk ()
mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = Label (text = "Welcome to MyMathDictionary. Press Next to continue.",
fg = "blue",bg = "white")
mLabel1.place (x= 150,y = 200)
button = Button (text = "Next", command = forget_page1 )
button.place(x = 275,y = 230)
mGui.mainloop()
I want the user to click on "next" and then a button appears caled "button1" after clicking on that button a text should appear "this is my text" but it gives me an error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "C:\Python33\Projects\MyMathDictionary.py", line 7, in hypoténusegetdef
widgets1 = button1
NameError: global name 'button1' is not defined
Any help would be apreciated :
button1 is defined in next_screen, but used in hypoténusegetdef -- you can't use a variable from one function inside another. Looking at the rest of the code, the simplest thing would probably be to use a global variable that can be accessed anywhere (generally bad practice, but for short scripts they can make things easier)

Categories

Resources