I have a gui that I wrote with tkinter and I want to call it again with a button how do I do this (They will be independent of each other)
I want to do like this:
import tkinter
root = Tk()
root.title("test")
def testt()
root()
Button(root, text='window +', command=testt).pack()
root.mainloop()
You also have to import some modules as shown.
Try this:
from tkinter import Tk, Button, Toplevel
def testt():
root1 = Toplevel()
root1.mainloop()
Just add TopLevel in test() function.
Code:
import tkinter as tk
root = tk.Tk()
root.title("test")
def testt():
root1 = tk.Toplevel()
tk.Button(root, text='window +', command=testt).pack()
root.mainloop()
Screenshot output:
Related
I'm trying to make a button that opens another python file
import os
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('juststop')
os.startfile(r'C:\Users\75259\PycharmProjects\pythonProject9\main.py')
open_button = ttk.Button(
root,
text='calculater',
)
open_button.pack(
ipadx=5,
ipady=5,
expand=True
)
root.mainloop()
I have tried a multitude of things but non seem to work
The way you have your os.startfile implemented calls the function right away. One way to solve this is to create a function to use it when called and link it to your button. Below is a modification of your example that will only open the file when you press the calculater button.
import os
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('juststop')
def open_file():
os.startfile(r'C:\Users\75259\PycharmProjects\pythonProject9\main.py')
open_button = ttk.Button(root, text='calculater', command=open_file)
open_button.pack(ipadx=5, ipady=5, expand=True)
root.mainloop()
I am trying to create a GUI in Python 3.9.1 with tkinter and am having issues getting a default value to show in an Entry box.
from tkinter import *
from tkinter import ttk
class GUI:
def __init__(self, root, *args):
self.root = root
self.initial_frame = ttk.Frame(self.root)
self.initial_frame.grid(row=0)
ttk.Label(self.initial_frame, text="User question here?").grid(row=1, column=1)
self.ext_len = StringVar(value=4)
ext_len_entry = ttk.Entry(self.initial_frame, textvariable=self.ext_len)
ext_len_entry.grid(row=1, column=2)
root = Tk()
GUI(root)
root.mainloop()
Note: official tkinter docs say to use from tkinter import * and from tkinter import ttk.
See this link and look in the Python section in the step-by-step walkthrough https://tkdocs.com/tutorial/firstexample.html
I've also tried using self.ext_len.set(4) after initializing the entry but before putting it on the grid. I've tried self.ext_len.insert(0,4) as well.
If I add a button with a callback that does nothing, the 4 shows up in the entry box. I found that I don't even have to render it on the grid. Just initializing it is enough. A button without a callback does not work.
Working code:
from tkinter import *
from tkinter import ttk
class GUI:
def __init__(self, root, *args):
self.root = root
self.initial_frame = ttk.Frame(self.root)
self.initial_frame.grid(row=0)
ttk.Label(self.initial_frame, text="User question here?").grid(row=1, column=1)
self.ext_len = StringVar(value=4)
ext_len_entry = ttk.Entry(self.initial_frame, textvariable=self.ext_len)
ext_len_entry.grid(row=1, column=2)
ttk.Button(self.initial_frame, text="Test button", command=self.test_func)
def test_func(self, *args):
pass
root = Tk()
GUI(root)
root.mainloop()
Why does initializing the button cause it to work?
I'm getting this issue as mentioned in the title.
I'm trying to run a file to print hello world in the widget. I'm getting what I want when I run it in my system, but when I'm running it in colab its not working.
Code:
import tkinter
root = tk()
myLabel = Label(root, text="Hello World!")
myLabel.pack()
root.mainloop()
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-db74150bd164> in <module>()
1 import tkinter
2
----> 3 root = tk()
4
5 myLabel = Label(root, text="Hello World!")
TypeError: 'module' object is not callable
I tried changing tk into various forms(Tk, tK, Tkinter, tKinter), but it isn't working anyhow.
When you see Tk(), it is an instance of Tk() class present in __init__.py file in tkinter folder.
Since you have imported tkinter, you have to specify tkinter.Tk()to create a instance of Tk()
import tkinter
root = tkinter.tk()
myLabel = Label(root, text="Hello World!")
myLabel.pack()
root.mainloop()
In some programs, you can also see tk.Tk(). This is because the module tkinter is imported as tk:
import tkinter as tk
See the source code of tkinter
At line 2273 in __init__.py, you can see:
class Tk(Misc, Wm):
"""Toplevel widget of Tk which represents mostly the main window
of an application. It has an associated Tcl interpreter."""
_w = '.'
def __init__(self, screenName=None, baseName=None, className='Tk',
useTk=True, sync=False, use=None):
...
There are some errors:
import tkinter as tk
root = tk.Tk() # tk() you can't call a module, write tk.Tk() instead.
myLabel = tk.Label(root, text="Hello World!") # add tk.
myLabel.pack()
root.mainloop()
you can just import tkinter as
from tkinter import *
by using this it will work
I want to have abutton like this in tkinter (Modern window 10 buttons):
However, I get this button:
The code for my button is:
from tkinter import Tk,Button
root=Tk()
Button(root,text='OK').pack()
Another alternative to create button is to create a label and bind it to the action functions. In the below example .bind() is used to connect the label with respective function. You can design according to your requirements.
from tkinter import *
def OnPressed(event):
print('Hello')
def OnHover(event):
But.config(bg='red', fg='white')
def OnLeave(event):
But.config(bg='white', fg='black')
root = Tk()
But = Label(root, text='Hi', bg='white', relief='groove')
But.place(x=10, y=10, width=100)
But.bind('<Button-1>', OnPressed)
But.bind('<Enter>', OnHover)
But.bind('<Leave>', OnLeave)
root.mainloop()
The themed widgets are in 'themed Tk' aka ttk.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
ok = ttk.Button(root, text='OK')
ok.pack()
root.mainloop()
Avoid from tkinter import * as both tkinter and tkinter.ttk define Button and many other widgets.
If you use this on Windows you should get something looking like a native button. But this is a theme and can be changed. On Linux or MacOS you will get a button style that is appropriate to that platform.
vol_up = Button(root, text="+",activebackground='orange',bg='yellow')
vol_up.pack(side='top')
I use this to get yes/no from user but it opens an empty window:
from Tkinter import *
from tkMessageBox import *
if askyesno('Verify', 'Really quit?'):
print "ok"
And this empty window doesnt go away. How can I prevent this?
This won't work:
Tk().withdraw()
showinfo('OK', 'Select month')
print "line 677"
root = Tk()
root.title("Report month")
months = ["Jan","Feb","Mar"]
sel_list = []
print "line 682"
def get_sel():
sel_list.append(Lb1.curselection())
root.destroy()
def cancel():
root.destroy()
B = Button(root, text ="OK", command = get_sel)
C = Button(root, text ="Cancel", command = cancel)
Lb1 = Listbox(root, selectmode=SINGLE)
for i,j in enumerate(months):
Lb1.insert(i,j)
Lb1.pack()
B.pack()
C.pack()
print "line 702"
root.mainloop()
for i in sel_list[0]:
print months[int(i)]
return months[int(sel_list[0][0])]
Tkinter requires that a root window exist before you can create any other widgets, windows or dialogs. If you try to create a dialog before creating a root window, tkinter will automatically create the root window for you.
The solution is to explicitly create a root window, then withdraw it if you don't want it to be visible.
You should always create exactly one instance of Tk, and your program should be designed to exit when that window is destroyed.
Create root window explicitly, then withdraw.
from Tkinter import *
from tkMessageBox import *
Tk().withdraw()
askyesno('Verify', 'Really quit?')
Not beautiful solution, but it works.
UPDATE
Do not create the second Tk window.
from Tkinter import *
from tkMessageBox import *
root = Tk()
root.withdraw()
showinfo('OK', 'Please choose')
root.deiconify()
# Do not create another Tk window. reuse root.
root.title("Report month")
...