I'm rather unfamiliar with Python, but am attempting to make a simple gui with tkinter (Python 2.7.x). I have one fully functional button, but I can only get the script to work in iPython. When I try to run it outside of the iPython environment, I see some text run down the command prompt but nothing happens (ie no gui appears, no action happens).
My code:
import Tkinter
from Tkinter import *
import os
#define frame
root = Tk()
frame = Frame(root)
frame.pack()
#define buttons
button = Button(frame, text="Action", command= lambda: os.system("Action.py"))
button.pack(side=LEFT)
Add root.mainloop() to the end of your code.
Related
im try to make restart button for my python program gui My gui but im still confused how to do it. Anyone know how to make the button to restart python code ? I appreciate every help.
use the code below:
import sys
import os
from tkinter import Tk, Label, Button
def restart_me():
pth = sys.executable
os.execl(pth, pth, *sys.argv)
root = Tk()
Label(root, text="hi there!").pack()
Button(root, text="Restart", command=restart_me).pack()
root.mainloop()
I loaded up Anaconda and realized there is more stuff there than I can absorb in my lifetime. However, I did load Spyder and pasted in a working program that runs without error in PyCharm and Thonny.
"""Create a 2 button window"""
import tkinter as tk
from tkinter import *
from tkinter import messagebox
#write slogan out in a message box
def write_slogan():
messagebox.showinfo("our message",
"tkinter is easy to use")
#set up the window
root = tk.Tk() #get the window
root.geometry("100x100+300+300") #x, y window size and position
#create Hello button
slogan = Button(root,
text="Hello",
command=write_slogan)
slogan.pack(side=LEFT, padx=10)
#create exit button with red letters
button = Button(root,
text="QUIT",
fg="red",
command=quit)
button.pack(side=RIGHT, padx=10)
#start running the tkinter loop
root.mainloop()
It may not surprise Spyder mavens, but I was surprised to see that it did not recognize "quit" However, I replaced it with
command=sys.exit
and that at least compiled.
However, when I ran it, the window appeared UNDER the Spyder window. I found it in the task bar. BUT, when I clicked in the Quit button, the program just hung. I had to go to the console and and type
quit
to stop the process.
I guess Spyder isn't really intended for GUI programming.
When I call the basic command:
from tkinter.filedialog import askopenfilename
filename = askopenfilename()
A little window pops up behind the selector screen.
Is there a way to remove this?
It pops up because you need at least a "root" window for each tkinter application. In your case, you aren't creating any root window explicitly, so askopenfilename creates it automatically for you. One solution would be to create the root window explicitly and then hide it, something as follows
from tkinter.filedialog import askopenfilename
from tkinter import Tk
Tk().withdraw()
filename = askopenfilename()
I am creating a game to run inside a GUI (text area, button, menu etc) I've created a GUI with wxpython. I create a panel inside the main window, which runs a pygame thread.
Problem:
On Windows, the pygame thread runs perfectly inside the main window. But on Linux, the pygame pop up on a new window. How can I set this such that both windows and Linux run the thread in the main window?
Code:
class SDLPanel(wx.Panel):
def __init__(self,parent,ID,tplSize):
global pygame
global pygame_init_flag
wx.Panel.__init__(self, parent, ID, size=tplSize)
self.Fit()
if (sys.platform == 'win32'):
os.environ['SDL_WINDOWID'] = str(self.GetHandle())
os.environ['SDL_VIDEODRIVER'] = 'windib'
else:
os.environ['SDL_VIDEODRIVER'] = 'x11'
#here is where things change if pygame has already been initialized
#we need to do so again
if pygame_init_flag:
#call pygame.init() on subsaquent windows
pygame.init()
else:
#import if this is the first time
import pygame
pygame_init_flag = True #make sure we know that pygame has been imported
pygame.display.init()
window = pygame.display.set_mode(tplSize)
self.thread = SDLThread(window)
self.thread.Start()
def __del__(self):
self.thread.Stop()
print "thread stoped"
#very important line, this makes sure that pygame exits before we
#reinitialize it other wise we get errors
pygame.quit()
Solved problem.
In main window we must self.Show()
Idk why in linux the main window must be showed . Same code.
Tks all
This is a disclaimer alert, according to https://forums.libsdl.org/viewtopic.php?p=39332, the solution works only with SDL 1.2 and not 2.0.
I am trying to create a shell script in which I automatically run a video fullscreen.
It has no way to quit unless I shut down the Raspberry Pi.
What is a small script I can use to bind something like "!" to quit the application?
I searched Google for 'omxplayer exit fullscreen' and found this answer, originally posted by dom on the RaspberryPi forum:
Changing TV modes does lose any content on them (e.g. the console
framebuffer).
You can provoke the console framebuffer to be recreated with: fbset
-depth 8 && fbset -depth 16
Add that to the end of a script that launches omxplayer.
(for extra points read the depth before launching omxplayer and set it
back to original value afterwards)
You might also want to check this issue report on omxplayer's GitHub.
I am not sure if this will work but potentially you could use a tkinter window that you make non visable.
#import the tkinter module for the GUI and input control
try:
# for Python2
import Tkinter as tk
from Tkinter import *
except ImportError:
# for Python3
import tkinter as tk
from tkinter import *
def key(event):
#create a function to control closing the window in this case
if event.keysym == 'Escape':
#this currently closes the window however you could add to root.destroy() with
#the relevant command for closing the video.
root.destroy()
#initiate root window, remove it from view, bind all keys (you could just
#bind '<Escape>' if preffered
root = Tk.tk
root.withdraW()
root.bind_all('<Key>', key)
i am aware this isnt a specific design to your problem however it will let you bind the escape key like you wished. this stopped the whole application in my example however you may have to include extra lines to ensure each part of your application is correctly ended.