How do I run a program within a Tkinter frame? - python

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.

Related

tkinter fill=Y does not work with Button [duplicate]

I am familiarizing myself with Tkinter, and I am attempting to write a very simple program, which displays a button in a window, using the pack geometry manager.
I was experimenting with various configuration options for pack(), such as expand, fill, and side, and I've run into a peculiar problem. I have written the following code:
from Tkinter import *
root = Tk()
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()
The problem is that the button expands to fill the window in the horizontal direction, but not the vertical direction. This is the same result that I get if instead of specifying fill=BOTH I use fill=X. In addition, if I specify instead fill=Y the button does not expand in either direction. Something seems to be going wrong with the fill in the vertical direction, and I cannot figure out what it might be.
I attempted to Google this problem and surprisingly found no mention of this happening to anyone else. I am using a Mac with OS X Yosemite and running python 2.7.5. I also attempted to compile with python 3.4.1 and saw no change.
Edit:
Based off of the answer and comments below, it is clear that there is nothing wrong with my code, because it seems to work on other machines. If not an error in the code, does anyone know what could possibly be causing the button to not stretch vertically when I run the above code?
This is a feature of native buttons on OSX. Buttons on OSX will be a fixed height and will not expand vertically. There is nothing you can do, short of using a different widget such as a label.
try running this code to see the behavior of fill and expand
from Tkinter import *
root = Tk()
root.geometry("500x500")
widget = Button(root, text='text1')
widget.pack(fill=X, expand=1)
widget = Button(root, text='text2')
widget.pack(fill=Y, expand=1)
widget = Button(root, text='text3')
widget.pack(fill=BOTH, expand=1)
root.mainloop()
Argument fill does fill in vertical direction as well
I am also beginner, defining geometry for fill was missing in your code as given below:
from Tkinter import *
root = Tk()
root.geometry("600x400")
widget = Button(root, text='text')
widget.pack(expand=YES, fill=BOTH)
root.mainloop()

Python Tkinter updating text-box content via a function

Beginner programmer here, working on making a basic GUI as part of a tutorial I was following online, but none of them say how to get a Text box to update using the output of the other parts of your code.
I tried multiple other answers on the site, including one using StringVar's, which got me nowhere, another using a decorator, and the rest seemed way out of my depth.
Here's my code:
import tkinter as tk
import time
#Creating Root
root = tk.Tk()
#GUI TEMPLATE
frame =tk.Frame(root,
height = 100,
width = 400)
frame.pack()
v = StringVar()
colour = ["red","blue","green","white","yellow"]
labels = range(5)
#change number to change how many labels
for i in range(5):
l= tk.Label(root,
text = colour[i],
bg = colour[i])
l.place(x = 10 +i*70, y = 10, width=60, height=25)
T1 = tk.Text(root, height=2, width=40)
words = "Don't name your files after module names!"
T1.insert(tk.END, textvariable=v)
T1.place(x = 10, y= 40)
S = tk.Scrollbar(root)
S.config(command=T1.yview)
S.place(x = 340, y=40)
T1.config(yscrollcommand=S.set)
root.mainloop()
v.set("Something Else!")
Now, what it should output is a row of coloured labels, which works fine, and a text box with a scroll bar, which should instantly update to read 'Something Else!', which does not work fine.
Instead I get the following error:
NameError: name 'StringVar' is not defined
I know what this error means, it's just I've hit a wall when it comes to finding a solution that works for me, and doesn't need a doctorate to understand.
What I'm asking for is if someone can give me a solution that would work for this, and hopefully explain it!
Thanks in advance.
EDIT:
So after fixing the syntax error, and then finding out what I'm trying to do doesn't work, how would I go about this?
Could I use a label instead? Or is there another, better way?
Thanks again!
StringVar should accessed via tk:
v = tk.StringVar()
On another note, tk.Text.insert does not take a textvariable parameter, so the following won't work:
T1.insert(tk.END, textvariable=v)
# ^^^^^^^^^^^^??
From the docs:
Unlike for example the entry widget, text widgets don't support a
"textvariable" configuration option
Also see How can I connect a StringVar to a Text widget in Python/Tkinter? as to why this won't work.
I was experimenting with learning threading with Tkinter and have made something work to insert and update text in a Tkinter text box in Python3 which may be of help.
I found that by deleting the text I could then insert new text. Untill I deleted the existing text the instruction to insert text seamed to be ignored.
{ foo = input("Give me input: " )
self.T.delete("1.0", END) #Clear the text window so we can write.
self.T.insert(END,foo) #Write the new text.}

How to include a command line interface in a gui in python

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)

How to correctly use Control Variable in Tkinter?

I just started to look into tkinter to develop some simple GUI applications. However, I had a hard time figuring out how the control variables are used. I simply want to retrieve the status of a checkbutton(whether it is checked) and the text of an entry, but using the control variable seems not doing the job. I know my_entry.get() can also be used to get the text, but since most of the tutorials use control variables, I believe it is worth learning to use it correctly... Really appreciate your help!
here is the sample code I had:
import tkinter as tk
def checkbtn_callback():
print(btn_state.get())
print(entry_text.get())
top = tk.Tk()
btn_state = tk.IntVar()
checkbtn=tk.Checkbutton(top,text='testbutton',variable=btn_state,command=checkbtn_callback)
checkbtn.grid()
entry_text = tk.StringVar()
entry = tk.Entry(top, text='text entry',textvariable=entry_text)
entry.grid()
tk.mainloop()

Good-styled console input

I'm writing a little Chatserver/-client to learn Python.
Now I want to make the consoleinput a little bit nicer, but I don't know how to do it...
Everytime I recieve a message from the socket, I do print() in the listening thread.
But then the text already entered to input() is over the printed message and the cursor is at the bottom.
What can I do, that is works like in Minecraft-Server, so the text already entered moves to the bottom?
Would be great if someone can help :)
You can't get that level of control with the console, but you can use python's default tkinter to make a simple UI. Below is an example (Python 3) that I whipped up in a few minutes. You can type in messages, press send, and they will appear in the box above.
from tkinter import *
from tkinter import ttk
def send(view, entry):
view.insert('1.0', entry.get() + "\n")
root = Tk()
msgview = Text(root, width=100, height=20)
msgview.grid(sticky=(N,E,S,W))
mymessage = StringVar(value="type here...")
msginput = Entry(root, textvariable=mymessage)
msginput.grid(sticky=(E,W))
sendbutton = ttk.Button(root, text="send",\
command=lambda: send(msgview, msginput))
sendbutton.grid()
root.mainloop()
I suggest looking at the tkdocs tutorial over the effbot one, since it is clearer, easier to follow and is more thorough in my opinion. New Mexico Tech also provides a great reference for tkinter here

Categories