I'm currently building an console application in python that does a lot of calculations on input gotten from the user and i've seen several apps that have gui windows but the input is still gotten from the console and this is exactly what i want, i'm currently learning tkinter so i wanted to ask is it possible to do this in tkinter because the tutorial i have on tkinter dosen't talk about including consoles in gui's here is a screenshot of what i mean https://www.imageupload.co.uk/image/ZEXe though in the screenshot the command line just prints out what is going on in the program but i want my main application to be in that area, so i can get input from the user and then the results can be printed out on the gui. Answers can also suggest other python gui frameworks. Thanks
Tkinter can accept text inputs and return values based on what is put in. As far as I know there is no actual command line input function. You could always try implementing it yourself. I think this is very customizable so I'm not going to write it myself but these are some of the things u might use:
textbox = Entry(root)
textbox.pack()
textbox.config(bg="red") #this would change the background to black. fg for text color
textarea = Text(root) #this creates a big text area
#textbox.get() will return the value of the textbook
Also, you could embed a full terminal into a tkinter window by using xterm (you might need to install it first if it's not natively available on your machine.
from tkinter import *
import os
root = Tk()
termf = Frame(root, height=500, width=500)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
os.system('xterm -into %d -geometry 100x100 -sb &' % wid)
root.mainloop()
This creates a new tkinter window (500 x 500) and them embeds an xterm window into it (100 x 100)
Related
Advice: i'm working on Windows 8.1, with Python and Tkinter at last version; i wrote from tkinter import * to post less code here, i know that it is a bad practice, use for example import tkinter as tk instead.
I'm coding my first text editor, and i have a problem when i resize the main windows. When i run the program it display on screen a window with dimension 750x500 pixel. So far, all ok, i can write text without problem (note that menu_bar and other features are work-in progress, but we dont care about them). Problem is with Text widget when user tries to resize window with cursor. The content of the text practically adapts to the size of the window (the length of each string is reduced or increased based on the width of the window). But i don't want that this happen. I want that Text widget changes his width automatically in base of window size, but the content mustn't be adapted. I hope that you understand my question, if not, i will try to explain better.
I have searched on online reference if there's a parameter to set this option, but i haven't found anything.
How to solve the problems concerning the Text widget and resizing the window?
from tkinter import *
root = Tk()
root.geometry("750x500")
content_text = Text(root, wrap=WORD, bg="grey25", undo=True, cursor="",
insertbackground="red", foreground="white", font="courier 12")
content_text.pack(fill=BOTH, expand=True)
scroll_bar = Scrollbar(content_text)
content_text.configure(yscrollcommand=scroll_bar.set, selectbackground="dodgerblue")
scroll_bar.configure(command=content_text.yview)
scroll_bar.pack(side=RIGHT, fill=Y)
if __name__ == '__main__':
root.mainloop()
You can't do what you want. If you have wrapping turned on, text will always wrap at the edge of the window. When you change the width of the window, the text will re-wrap to the new width. There is no configuration option to tell the widget to wrap at any other place.
Currently, I'm working with Python 2.7.10, specifically the Tkinter module, to create a GUI (I'm relatively new to Tkinter). As such, while my goal is to have the GUI print figures with matplotlib, I'm currently just testing the basics with print() to my iPython console (in Spyder). Anyways, a simple version of my code follows:
import Tkinter as tk
def run_file(i,f):
print 'running...'+`i`+', '+`f`
def select_all():
print('run')
run_file(1,97)
root = tk.Tk()
root.geometry('700x100')
singleframe = tk.Frame(root, bd = 5)
singleframe.pack()
button_fig = tk.Button(singleframe, text='Display All Figures', fg =
'black', padx = 2, command = select_all)
button_fig.pack(side = tk.LEFT)
root.mainloop()
Here's my problem:
Whenever I click my 'Display All Figures' button, the console remains blank 1, however, if I click it a second time, it displays 'run', and 'running...1, 97' (for my first click), and then displays 'run' (for the second click) 2, waiting for me to click it a third time (where it will display the second click's 'running...1, 97' result along with the third click's 'run'). Finally, if I close the program, it will display the 'running...1, 97' from my third click. (My apologies for not including more pictures; somehow I apparently have reputation below 10)
I've been trying to search the web for similar problems; several posts suggest that problems like this occur when too many calculations are occurring, so the GUI freezes up, but my function is very simple, so that seems unlikely; still others suggest the after() function, but I don't see how creating timed delays would help either. Does anyone have any suggestions as to how to solve this problem?
I'm building a small python 3 tkinter program on windows (10). From a main window (root) I'm creating a secondary window (wdowViewer). I want it to be full screen (zoomed) on my secondary display. The code below works on my main setup with two identical screens. If I however take my laptop out of the dock and connect it to (any) external display, the new window only fills about 2/3 of the secondary display.
Two things to note:
- The laptop and external monitor have same resolution.
- The window is appropriately zoomed when overrideredirect is set to 0.
mon_primary_width = str(app.root.winfo_screenwidth()) # width of primary screen
self.wdowViewer = Toplevel(app.root) # create new window
self.wdowViewer.geometry('10x10+' + mon_primary_width + '+0') # move it to the secondary screen
self.wdowViewer.wm_state('zoomed') # full screen
self.wdowViewer.overrideredirect(1) # remove tool bar
app.root.update_idletasks() # Apply changes
After two days of experimenting I finally found a fix.
Consider the following example: Making a toplevel zoomed, works fine on any secondary display when using the following code:
from tkinter import *
# Make tkinter window
root = Tk()
sw = str(root.winfo_screenwidth())
Label(root, text="Hello Main Display").pack()
# Make a new toplevel
w = Toplevel(root)
w.geometry("0x0+" + sw + "+0")
w.state('zoomed')
w.overrideredirect(1)
Label(w, text='Hello Secondary Display').pack()
root.mainloop()
However, in my code I'm making a new Toplevel after running the mainloop command. Then, the issue arises. A code example with the issue:
from tkinter import *
# New Tkinter
root = Tk()
sw = str(root.winfo_screenwidth())
# Function for new toplevel
def new_wdow():
w = Toplevel(root)
w.geometry("0x0+" + sw + "+0")
w.state('zoomed')
w.overrideredirect(1)
Label(w, text='Hello Secondary Display').pack()
# Make button in main window
Button(root, text="Hello", command=new_wdow).pack()
root.mainloop()
Problem: The bug is only present if the DPI scaling in Windows is not set to 100%. Hence, there seems to be a bug in how tkinter handles the DPI scaling subsequent to running mainloop. It does not scale the window properly, but Windows is treating it as if it does.
Fix: Tell Windows that the app takes care of DPI scaling all by itself, and to not resize the window. This is achievable using ctypes. Put the following code early in your python script:
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(2)
Hopefully others will find the solution helpful. Hopefully, someone may explain more of what is going on here!
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/