I'm new to tkinter in python and am not able to figure out what the syntax below does exactly.
oldtitle=window.newtitle()
Removing this line from the code doesn't makes any difference to the output.
from tkinter import *
from tkinter import ttk
root=Tk()
root.title('to')
main=Toplevel(root)
tk=main.title()#<---this line
main.title('hello world')
mainloop()
What the line tk=main.title() does is to get the title of main.
Here is a demo using your own code:
from tkinter import *
from tkinter import ttk
root=Tk()
root.title('to')
main=Toplevel(root)
main.title('hello world')
tk=main.title() # Note I moved this line to here
print(tk) # This will print 'hello world'
mainloop()
The line print(tk) will print the title of main which is hello world.
If you want to set a different title then use this synatax instead: tk = main.title('Some new title') (or simply main.title('Some new title') if you do not need to save the title string into an other variable):
from tkinter import *
from tkinter import ttk
root=Tk()
root.title('to')
main=Toplevel(root)
main.title('hello world')
tk=main.title('Some new title') # or simply: main.title('Some new title')
mainloop()
Output:
Note: avoid using tk as your personal variable name because the recommended way to import tkinter is: import tkinter as tk
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
In the following code I get an error when leaving out "from tkinter import *. I am confused as to the reason as I import tkinter as tk.
I tried modifying the function set_text without success.
The error is on
self.e.delete(0,END)
NameError: name 'END' is not defined`
Code:
from tkinter import *
import tkinter as tk
from tkinter import ttk
class HelperFun():
def set_text(self,parent,text):
self.e.delete(0,END)
self.e.insert(0,text)
return
def __init__(self,parent):
self.parent=parent
self.e = tk.Entry(self.parent,width=10)
self.e.pack()
self.b1 = tk.Button(self.parent,text="animal",
command=lambda:self.set_text(self.parent,"animal"))
...
root=tk.Tk()
HelperFun(root)
root.mainloop()
Thank you.
When you use, from Tkinter import *, it imports all of the constants from that package, like END.
To get away without using this, you'd need to use the dot operator, like tk.END, otherwise it won't be defined.
If you import tkinter as tk then you need to refer to END as tk.END.
Importing from tkinter import * imports everything in tkinter, making it so you don't have to use the tk qualifying prefix.
replace the line
self.e.delete(0,END)
with
self.e.delete(0,"end")
Your code isn't doing anything. And it is not showing Button
You cannot use self.e simultaneously in the set text() function.
The Button command isn't right.
I modified the code to make it more readability:
import tkinter as tk
class HelperFun():
def __init__(self, parent):
self.parent=parent
self.e = tk.Entry(self.parent,width=10)
self.e.pack()
self.b1 = tk.Button(self.parent,text="animal",
command=lambda:self.set_text(self.e.get()))
self.b1.pack()
self.lb = tk.Label(self.parent, width=10)
self.lb.pack()
def set_text(self, _text):
print(_text)
self.lb.configure(text=_text)
return
root=tk.Tk()
app = HelperFun(root)
root.mainloop()
Screenshot before and after clicking button:
I think in "(0,END)" you need to use 1.0 instead of 0.
Try "(1.0,END)"
END is like Button and Label ...
u have to write tk.END if u (import tkinter as tk)
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 have ScrolledText in Tkiner application ,in which i am supposed to write the python code in it and i have one button,by pressing that i send those lines of code to InteractiveInterpreter().runcode("code as a string")
This runcode() method return the NoneType object and shows output on the cmd
but i want output in string format, i tried using exec() but it didn't work for me.There is some way to do that?
import Tkinter as tk
from Tkinter import *
from ScrolledText import *
from code import InteractiveInterpreter
interpreter = InteractiveInterpreter()
def go(event):
print(interpreter.runcode(textPad.get('1.0', END+'-1c')))
root = tk.Tk()
textPad = ScrolledText(root,width=100,height=100)
textPad.focus_set()
b2=tk.Button(root, text ="Run",width=34,height=3)
b2.pack()
b2.bind('<Button-1>', go)
textPad.pack()
root.mainloop()
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