MacOSX Tkinter no response when Tk() - python

I have a TkButton.py like this
from Tkinter import *
root = Tk()
button = Button(root, text = 'Python/Tkinter')
button.pack()
root.mainloop()
But when I execute python2.7 TkButton.py, it just stuck.
I installed Python2.7 and py27-tkinter using MacPorts.
My OS is OS X 10.11.6
Can somebody help me?
Addition
Tkinter.Tk() not working. I don't know why.
And I can't write any command after root = tk.Tk(), so I have to Ctrl-C to quit.

Related

How will I make a tk window which cannot be minimized in Python Tkinter?

I tried win.overrideredirect(True) it didn't work...
My Code goes like this:
from tkinter import *
win = Tk()
win.resizable(0,0)
win.wm_protocol("WM_SAVE_YOURSELF", lambda: print("On exit"))
Label(win, text="Tk Window").pack()
win.mainloop()
Specs:
Python 3.9.6 [Latest],
Pip 21.1.3,
OS: Windows 10 Home
I want to make the minimize button to be disabled...
Please help
The thing #N1CK145 posted didn't work for me but I am guessing that he/she tried to do this:
import Tkinter as tk
root = tk.Tk()
root.attributes("-toolwindow", True)
root.mainloop()
If you want to know more attribute options look here
try resizeable function like this :
win= Tk()
win.geometry("750x250")
win.resizable(False, False)
Found this here:
import Tkinter as tk
root= tk.Tk()
root.title("wm min/max")
# this removes the maximize button
root.resizable(0,0)
# # if on MS Windows, this might do the trick,
# # but I wouldn't know:
# root.attributes(toolwindow=1)
# # for no window manager decorations at all:
# root.overrideredirect(1)
# # useful for something like a splash screen
root.mainloop()

Tkinter code Works in Windows but not in Mac

Why does the following test not display the menu on my Mac running Python 3.7 on Mojave?
Tried a shebang but that didn't help.
import tkinter as tk
def quit_app():
my_window.destroy()
my_window = tk.Tk()
my_menu = tk.Menu(my_window)
my_menu.add_command(label='Quit',command=quit_app)
my_window.config(menu=my_menu)
my_window.mainloop()
The menu displays in Windows 10 but not on the Mac. The tkinter window is blank on the Mac.
The menu displays in Windows 10 but not on the Mac
Yes, that is because MAC has a design philosophy that all devs for their platform have to conform with.
You cannot have a add_command as a top level menu item on mac, rather:
import tkinter as tk
def quit_app():
my_window.destroy()
my_window = tk.Tk()
my_menu = tk.Menu(my_window)
quit_menu= tk.Menu(my_menu, tearoff=0)
quit_menu.add_command(label='Quit App',command=quit_app)
my_menu.add_cascade(label="Quit", menu=quit_menu, underline=0)
my_window.config(menu=my_menu)
my_window.mainloop()

Tkinter error in pycharm : root is not defined

Hi I am a novice programmer.I am trying to use tkinter in pycharm....
When i try to use root or any other function it doesn't show any suggestion and gives not defined error.The program works fine in idle. But cann't seem to get it work with pycharm.Please help.I have already installed tkinter package and its also enabled in project interpreter settings....
here's what I am trying to do...
from tkinter import *
root = Tk()
topframe = Frame(root)
topframe.pack()
bottomframe = Frame(root)
bottomframe.pack(side = BOTTOM)
button1 = Button(topframe, text='Button 1', fg='red')
button2 = Button(topframe, text='Button 2', fg='blue')
button3 = Button(bottomframe, text='Button 3', fg='green')
button1.pack(side = LEFT)
button2.pack(side = LEFT)
button3.pack()
root.mainloop()
Just try this
from tkinter import Tk
root= Tk()
I also faced this problem before. This worked for me. I don't know the reason.
Note Tkinter has been renamed to tkinter in Python 3
tkinter — Python interface to Tcl/Tk.(Tk itself is not part of Python; it is maintained at ActiveState)
source : official Doc
Also there are certain classes that don't get imported when you try to import with *. When working with tkinter.
turn Around:
Solution 1.
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
root = tk.Tk()
Solution 2:check correct version is installed in pycharm.
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
root.mainloop()
Solution 3: Absolute Import
from tkinter import Tk
root= Tk()
w = Label(root, text="Hello, world!")
I had the same problem. I found that when I typed from tkinter there were 2 different options; you need to choose the option with a file icon next to it

Why always have to force quit python launcher after running Tkinter on Mac?

I'm using jupyter notebook on mac, recently I need to write interactive dialog box, so after google, I use Tkinter to make an interactive window.
But I was bothered by this problem couples day ,and still can't find a solution way.
Fisrt example:
from Tkinter import *
from tkFileDialog import askopenfilename
import sys
import os,time
def callback():
name= askopenfilename()
print name
errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()
Second example:
from Tkinter import *
import sys,os
class YourApp(Tk):
def quit_and_close(self):
app.quit()
#os._exit(0)
#sys.exit(1)
#exit(0)
app = YourApp()
app.title('example')
app.geometry('400x300+200+200')
b = Button(app, text = "quit", command = app.quit_and_close)
b.pack()
app.mainloop()
And the third one:
import Tkinter as tk
import tkMessageBox
def ask_quit():
if tkMessageBox.askokcancel("Quit", "You want to quit now? *sniff*"):
root.destroy()
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", ask_quit)
root.mainloop()
After running those above code, always need have to force quit python launcher.
It is very weird, and annoying because after forcing quit, I will got the error:
Is it necessary to use python launcher as default window?
Is there possible to set another window to open ?
or is there proper way to close the launcher without causing programming crash?
p.s Even I try to use wxpython, it still open python launcher and got the same problem.

run a shell script from Python GUI

I've a GUI which will perform some functions when the buttons are pressed.
now i want to create a button in the GUI which will call and run a shell script in the background.
how can i achieve this ?
Not sure if your question is about how to call a shell script in Python, or how to make a button in your GUI. If the former, my comment above (recommending some research on subprocess.Popen) is the solution. Otherwise:
# assuming Python3
import tkinter as tk
import subprocess as sub
WINDOW_SIZE = "600x400"
root = tk.Tk()
root.geometry(WINDOW_SIZE)
tk.Button(root, text="Push me!", command=lambda: sub.call('path/to/script')).pack()
Python can run shell scripts using the supbprocess module. In order to run it in the background you can start it from a new thread.
To use the module
import subprocess
...
subprocess.call(['./yourScript.sh'])
For a good python threading resource you can try: How to use threading in Python?
Adding to what #lakesh said, below is the complete script :
import Tkinter
import subprocess
top = Tkinter.Tk()
def helloCallBack():
print "Below is the output from the shell script in terminal"
subprocess.call('./yourscript.sh', shell=True)
B = Tkinter.Button(top, text ="Hello", command = helloCallBack)
B.pack()
top.mainloop()
Please note that the shell script is in the same directory as that of the python script.
If needed, do chmod 777 yourscript.sh
subprocess.call('./yourscript.sh', shell=True)
and import Tkinter and not import tkinter solved the problems I was facing.
Use Tkinter to create a button. For more info, look at this video:http://www.youtube.com/watch?v=Qr60hWFyKHc
Example:
from Tkinter import *
root = Tk()
app = Frame(root)
app.grid()
button1 = Button(app,"Shell Script")
button1.grid()
root.mainloop()
To add the functionality:
change button1 line to:
button1 = Button(app,"Shell Script",command=runShellScript)
def runShellScript():
import subprocess
subprocess.call(['./yourScript.sh'])

Categories

Resources