I'm using Python 3, tkinter module in Windows 10. I know the question has been asked frequently, but none of the questions on this forum (or any other result from google) seems to have the answer. I tried to get started with tkinter (which I have never used before), but each really basic working example only returns the main tkinter GUI window and no widgets. I've tried to run most of the solutions to questions asked on this forum as well (like Python tkinter widgets not showing, Tkinter widgets not appearing or Tkinter widgets not showing), but the same result (just the main window, no adjustments to the main window or any widgets are showing).
My current MWE is:
from tkinter import *
from tkinter import messagebox
top = Tk()
top.geometry("100x100")
def helloCallBack():
msg = messagebox.showinfo( "Hello Python", "Hello World")
B = Button(top, text = "Hello", command = helloCallBack)
B.place(x = 50,y = 50)
top.mainloop()
I am following this tutorial. No errors show up.
The result is shown in the attached picture:
I run the code in cmd via Notepad++, which works fine for all normal python code not involving tkinter.
Try this:
B = Button(top, text = "Hello", command = helloCallBack).pack(top)
instead of:
B = Button(top, text = "Hello", command = helloCallBack)
B.place(x = 50,y = 50)
Place can be used to set where you want stuff to appear in a window.
Related
I am just learning python, and I am trying to make a window go full screen, which I have achieved, but I am now wanting to get rid of the title bar across the top. It currently looks like the image below, but I want it to also go over the Mac top toolbar at top (like a splash screen).
from tkinter import *
root = Tk()
root.attributes('-fullscreen', True)
root.attributes('-topmost', True)
root.overrideredirect(True)
def quitApp():
# mlabel = Label (root, text = 'Close').pack()
root.destroy()
# placing the button on my window
button = Button(text = 'QUIT', command = quitApp).pack()
I believe what you want to do is use
root.wm_attributes('-fullscreen','true')
Try this instead. It should do the trick.
from tkinter import *
root = Tk()
root.wm_attributes('-fullscreen','true')
def quitApp():
root.destroy()
button = Button(text = 'QUIT', command = quitApp).pack()
root.mainloop()
If this does not work because of the MacOS then take a look at this link This useful page has sever examples of how to manage mack windows in tkinter. And I believe what you may need to get borderless fullscreen.
This bit of code might be what you need:
root.tk.call("::tk::unsupported::MacWindowStyle", "style", root._w, "plain", "none")
Note: If you do use this option then you will need to remove root.wm_attributes('-fullscreen','true') from your code or just comment it out.
Update:
There is also another bit of code for tkinter 8.5+.
If you are using python with tkinter 8.5 or newer:
root.wm_attributes('-fullscreen', 1)
After googling and searching in SO, I come here.
I am making a program, but when I run it, the optionMenu widgets go to the end of the display, despite being set on the grid on the program. Here's a relevant example:
from tkinter import *
root=Tk()
root.title("Generador documentos")
app=Frame(root)
app.grid()
sexoPat=Label(app, text ="Gender")
sexoPat.grid(row=0,column=0)
var1 = StringVar()
sexoPatDrop= OptionMenu(root,var1,'Male','Female')
sexoPatDrop.grid(row=1,column=0)
sexoPatCheck=var1.get()
nombPat=Label(app, text ="name here")
nombPat.grid(row=2,column=0)
nombPatTXT=Entry(app)
nombPatTXT.grid(row=3,column=0)
In this sample code, I needed to write app instead of root in OptionMenu(root,...)
It was furas who gave me the answer.
I'm using tkinter to display a simple yesno messagebox in python 3.2.
The code is as follows:
x = tkinter.messagebox.askyesno("New Process", "New Process:\n" + e[2:-7] + "\n\nKill?")
Althought there is nothing wrong with the code(it functions as I want it to), there is a window in the background that appears and does not respond.
This window will crash after about a few seconds or after killing the host process.
What might cause this?
A couple of things:
It looks like you're not running it as a root window.
root = Tk()
app = Frame(root)
app.grid()
my_example = Label(app, "text")
my_example.grid()
root.mainloop()
You should put it in a bat file with pause and you'll be able to see the error
I'm trying to create a Tkinter app that incorporates the use of a touchscreen keyboard and will be run off a Raspberry Pi. I found an onscreen keyboard called Matchbox-keyboard.
My question is: is there a way to "embed" this keyboard into a GUI created by Tkinter? I would like to embed the keyboard so it opens at the bottom of the parent window.
So far all I can come up with is:
subprocess.Popen(['matchbox-keyboard'])
which works, but it opens in a separate window.
Below is a sample of my code. Keep in mind that I haven't coded the get() functions for the text fields yet, or any of the other functions for that matter.
from tkinter import *
from tkinter import ttk
import subprocess
process_one = subprocess.Popen(['matchbox-keyboard'])
root = Tk()
bottomframe = Frame(root)
bottomframe.pack(side = BOTTOM)
root.title("PinScore")
L0 = Label(root, text = "Welcome to PinScore!")
L0.pack(side = TOP)
L1 = Label(root, text = "Initials:")
L1.pack(side = LEFT)
E1 = Entry(root, bd = 5)
E1.pack(side = RIGHT)
L2 = Label(root, text = "High Score:")
L2.pack( side = RIGHT)
E2 = Entry(root, bd = 5)
E2.pack(side = RIGHT)
B = Button(bottomframe, text = "Enter High Score")
B.pack(side = BOTTOM)
root.mainloop()
The short answer: no, but there is hope, and it will require a fair amount of work. According to the github it is made in gtk. Then the question becomes "Can I put a gtk object in my tkinter program?". To my knowledge (and a lot of Googling) there is no way to embed gtk features in the Tkinter. You may want to try pyGTK instead, because these would be much easier to integrate (I know that it is possible). I might suggest that before you get any further in your project.
Use PyGTK! The github contains the gtk source and you can do it that way.
Actually, looking more at the github, you may not need to do that. The keyboard allows for command-line options, such as -v,--override Absolute positioning on the screen and -g,--geometry <HxW.y.x> Specify keyboard's geometry (taken from the github). You won't be able to control the z position (as in whether it is above or below your window).
If you truely want the embeded feeling, the github also says that you can embed it in gtk and points to examples/matchbox-keyboard-gtk-embed.c this might be what your looking for. You probably can translate it to pygtk. I found this, which talks about XEMBED. And I found this too which actually embeds something. Finally, I'll point you to the docs for gtk.socket.
I looked through a tutorial on using Tkinter and saw that the following code:
>>> from Tkinter import *
>>> win=Tk()
This should produce a box with the title Tk and nothing else. However, when I try this code out no such box appears. I'm not getting any errors so I suspect it's working as intended. Is it possible that there are additional steps I have to take if I'm on a mac?
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
This code runs automatically, however, in the guide it suggests that I use $ python hello1.py to run this code, which doesn't work. Any ideas on why this might be?
However, this larger block does not work:
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(
frame, text="QUIT", fg="red", command=frame.quit
)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
root.destroy() # optional; see description below
The issue seems to have something to do with mainloop but I'm confused because at the same time that earlier block worked just fine with a root.mainloop() part.
Do you run this code in IDLE?
Try above code in terminal (not in IDLE), then it will work as expected.
So if you want to try and run it in Terminal you should follow the steps below
note- 'I find that running programs is Terminal that involve a tkinter Gui will often crash for me, however it may work for you'
1st - Open Terminal
2nd - Type 'python3.4' then press space bar once
3rd - Open a Finder window
4th - Go to where you saved your python file in the Finder window
5th - Once you have located the file in Finder, drag the file into the Terminal window
6th - Press enter, and enjoy your python program.
another note - 'It sounds like you need a better Python IDE, you should try out PyCharm it is a great Python IDE which you can code and run python programs in including tkinter stuff'
You can download PyCharm here https://www.jetbrains.com/pycharm/