Message is not printed Tkinter - python

When the user didn't chose any option, the code going into else, in if_button_ispressed function, and then I want to print an error message on the screen. I think I did all correct but the message is not shown on the screen.
import sys
from Tkinter import *
import Image, ImageTk
import ShareScreen
import Menu_WatchAnothersScreen
def if_button_is_pressed():
global user_selection, mGui
if(user_selection.get() == 1): # if the user want to share his screen
mGui.destroy()
ShareScreen.run()
#menu()
if(user_selection.get() == 2): # if the user want to watch another`s screen
mGui.destroy()
Menu_WatchAnothersScreen.run()
#menu()
else: # if the user didn`t chose any option <--------------- HERE IS MY PROBLEM
error_message = "Please select one of the options" #<--------------- HERE IS MY PROBLEM
error_label = Label(mGui, textvariable=error_message).pack(anchor=CENTER) # prints error message <--------------- HERE IS MY PROBLEM
def close(): # close the window
exit()
def menu():
global user_selection,mGui
mGui = Tk()
user_selection = IntVar()
menubar = Menu(mGui) # menu
filemenu = Menu(menubar, tearoff=0) # menu works
filemenu.add_command(label="Close", command=close)
menubar.add_cascade(label="File", menu=filemenu)
mGui.geometry('450x300+500+300')
mGui.title('Nir`s ScreenShare') # top of window
canvas = Canvas(mGui, width=500, height=150)
canvas.pack(pady = 10)
pilImage = Image.open("logo5.png")
image = ImageTk.PhotoImage(pilImage)# puts ScreenirShare`s logo on the menu
imagesprite = canvas.create_image(0, 0, image=image, anchor="nw")
Radiobutton(mGui, text="Share My Screen ", variable=user_selection, value=1).pack(anchor=CENTER) # 1 - FIRST OPTION - Share My Screen
Radiobutton(mGui, text="Watch Another`s Screen", variable=user_selection, value=2).pack(anchor=CENTER, pady = 7.5)# 2- SECOND OPTION - Watch Another`s Screen
start_button = Button(mGui, text='Start', command=if_button_is_pressed).pack() # Start Button
mGui.config(menu=menubar) # menu helper
mGui.mainloop()
menu()

If you are going to use plain string, you should use text option of label not textvariable.
Label(mGui, text=error_message)
If you want to use textvariable, you need StringVar, IntVar etc.
Also, packing on same line returns None, so you should pack after you create it if you want to use it again later.
error_label = Label(mGui, textvariable=error_message)
error_label.pack(anchor=CENTER)

Related

tkiner output isnt on the same pop-up window

Okay so what I'm trying to figure out is how to control the location of output.
Currently when you run the code a window comes up and allows you to type into a label "manual". with the option to press enter or click the 'submit' button. The typed entry is then put in a new pop-up window every time I.e. Input1(test) and input2(testing) will show up in separate windows. How do I set it so it shows up in the same pop-up window preferably spaced on the y axis so it isn't cluttered. As this is a manual entry there is no telling how many entries will be submitted and it may be possible once so many entries have been entered it will need to move over on the X axis and start again at the top of the Y axis.
I'd also like to know how to pick where the pop-up ends up on the screen I tried using
win.place(x=200, y=50) but the .Toplevel() doesn't have an attribute of .place()
import tkinter as tk
#function that prints the entry
def entry_function(e=None):
name = entry.get()
win = tk.Toplevel()
tk.Label(win, text=name).pack()
win.geometry('100x100')
#Setup for the window
window = tk.Tk()
window.title("Title_Name")
window.geometry("500x500")
window.bind('<Return>', entry_function)
#widgets
label = tk.Label(window, text = "Manual:", bg = "dark grey", fg = "white")
entry = tk.Entry()
button = tk.Button(text = "submit",
command = entry_function)
#widget placement
label.place(x=0,y=20)
entry.place(x=52,y=21)
button.place(x=177, y=18)
window.mainloop()
For displaying the entry on same popup, you can use the below code for entry function
import tkinter as tk
flag = False
win=""
def setflag(event):
global flag
flag = False
#function that prints the entry
def entry_function(e=None):
global flag,win
name = entry.get()
if not flag:
win = tk.Toplevel()
win.geometry('100x100')
win.bind('<Destroy>', setflag)
tk.Label(win, text=name).pack()
flag = True
win.geometry("+%d+%d" % (x + 200, y + 50))
else:
tk.Label(win, text=name).pack()
#Setup for the window
window = tk.Tk()
window.title("Title_Name")
window.geometry("500x500")
window.bind('<Return>', entry_function)
x = window.winfo_x()
y = window.winfo_y()
sh = window.winfo_screenheight()
sw = window.winfo_screenwidth()
x_cord = int((sw/2)-550)
y_cord = int((sh/2)-300)
wh = 550
ww = 1100
window.geometry("{}x{}+{}+{}".format(ww, wh, x_cord, y_cord))
#widgets
label = tk.Label(window, text = "Manual:", bg = "dark grey", fg = "white")
entry = tk.Entry(window)
button = tk.Button(window,text = "submit",
command = entry_function)
#widget placement
label.place(x=0,y=20)
entry.place(x=52,y=21)
button.place(x=177, y=18)
window.mainloop()
You need to create the popup once and add label to it inside entry_function().
You can make the popup hidden initially and show it inside entry_function() and you can specify the position of the popup using geometry().
def entry_function(e=None):
MAX_ROWS = 20 # adjust this value to suit your case
name = entry.get().strip()
if name:
n = len(popup.winfo_children())
tk.Label(popup, text=name).grid(row=n%MAX_ROWS, column=n//MAX_ROWS, sticky='nsew', ipadx=2, ipady=2)
popup.deiconify() # show the popup
window.focus_force() # give focus back to main window
...
# create the popup
popup = tk.Toplevel()
popup.withdraw() # hidden initially
popup.geometry('+100+100') # place the popup to where ever you want
popup.title("Log Window")
popup.protocol('WM_DELETE_WINDOW', popup.withdraw) # hide popup instead of destroying it
...

on a button click, copy whats inside entry() and store it to a variable (tkinter)

I have two widgets to work with, a text input, and a button, both are created inside a function. What I want to happen is the user types in their name and then clicks the button to submit the answer. What I want the computer to do, is on the button press it will read whats inside the text and the save it to a variable. Once it saves it, it will print it out.
The code below is bad because it runs through the if statement immediately without the consulting of the button press.
There has to be a simpler solution. Also this may not be PEP 8 or whatever please be patient because I'm new.
import tkinter as tk
from tkinter import Tk, Label, Button
import sys
import time
import random
import threading
from tkinter import *
window = tk.Tk()
window.geometry("300x300")
window.title("GUI")
def start_screen():
reset()
start = tk.Label(window, text="start of game")
start.place(x=110,y=20)
play = Button(window, text= "play", command = start_game)
play.place(x=110,y=50)
helper = Button(window, text="help", command = help_screen)
helper.place(x=110,y=70)
def stuff():
global t
t = True
print(t)
return t
def text_handling():
global t
t = False
reset()#clears the screen
label = Label(window, text='')
question1= "what is your name?"
label.pack()
print_slow(label, question1, 40)#prints out letters slowly
#here is the part I'm having problems with
name = Entry(window)
name.pack()
but = Button(window, text="enter", command= stuff)
but.pack()
print(t)
if t == True:
myPlayer.name = name.get()
print(myPlayer.name)
def start_game():
reset()
bt = tk.Button(window,text="Enter", bg="orange", command =
text_handling)
bt.place(x=100,y=100)
start_screen()

Title bar menu in tkinter

I think most of us must have seen the command prompt(windows) and how when we open it and click on it's icon and it shows a menu. Can we do a similar thing with tkinter? This is not the normal menubar.
Here is an illustration of the command prompt one.
This is just an exampel of a work around without the need of doing all your window management by your own. Of course it will need improvements but as a start: Popup stolen from
import tkinter as tk
from PIL import Image, ImageTk
def popup(event):
popup_menu.tk_popup(event.x_root, event.y_root, 0)
def set_icon():
global top, popup
top = tk.Toplevel(root)
top.overrideredirect(1)
top.attributes('-topmost',True)
offset = 30
x,y = root.winfo_rootx(),root.winfo_rooty()-offset
width, height = offset,offset
top.geometry("%dx%d+%d+%d" % (width,height, x,y))
my_label = tk.Label(top, image=photo)
my_label.pack(fill='both')
global popup_menu
popup_menu = tk.Menu(top, tearoff=0)
popup_menu.add_command(label="Delete",
command=lambda :print('del'))
popup_menu.add_command(label="Select All",
command=lambda :print('sel'))
top.bind("<Button-1>", popup)
def grab(event):
top.geometry(f'+{event.x+10}+{event.y+2}')
root=tk.Tk()
ico = Image.open('prac_img/p2.png')
photo = ImageTk.PhotoImage(ico)
root.iconphoto(False,photo)
#root.wm_iconphoto(False,photo)
root.bind('<Configure>',grab)
root.update_idletasks()
set_icon()
root.mainloop()
Another way would be to code your own titlebar and the use of overrideredirecr(1) which will undecorate your window by the window manager of your system.

How to get the entry text and display in another entry?

I just want that when I type my name inside the entry box then appears in another entry with some add text. The idea is type in the entry below and after that it showed in the big entry.I was looking for this solution, but just found place in Label. I don't want in Label. The window is more big, must drag to show the entry. There's is a picture that i use in this script:
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
cat = Entry(root)
cat.place(x=48, y=25, width= 350, height=140)
user = Entry(root)
user.place(x=75, y=550)
btn = Button(root, text='START')
btn.place(x=220, y=410)
root.mainloop()
#
Ok, It works the way you told me,thank you!
But now i'm facing another problem.
The problem is when i insert the function of the game in the second window. I tested in one window and it works, but when i place the function in the second window gives an error when i press the "Start" button:
'''user_try = int(txt.get())
NameError: name 'txt' is not defined'''
When i press reset button gives another error:
'''user_try = int(txt.get())
NameError: name 'txt' is not defined'''
So i know that is missing definition, but i don't know how to make a reference for this command that it's in the second window. Like i said running with just one window the program works.
Maybe i should make using class, i don't know, but i wish to make this way that i started. However if there's no other way to do as i'm doing, let's go.
I just simplify the script here, actualy the main script is more bigger, so my idea is when open the program, there is a window and the user read the instructions about the game and proceed open the second window. The window have pictures and some hidden buttons in the next picture, so there will be an interactivity with the environment.
The guess number is just the beggining. After that there will be new challeges.
I'm very excited doing this, but i'm stuck in this point. The part one i finished, the pictures, the hidden buttons it's exacly the way i want, but the challenge stops here in this problem.
from tkinter import *
from PIL import Image, ImageTk, ImageSequence
import random
from tkinter import messagebox
pc = random.randint(1,10)
def reset():
global pc
pc = random.randint(1,10)
cat['text'] = 'Ok! Lets Try Again!'
def openwin2():
win1.withdraw()
win2 = Toplevel()
win2.geometry('350x300+180+100')
win2.title('second window')
txt = Entry(win2)
txt.place(x=10,y=10)
cat = Label(win2,wraplength=300)
cat.place(x=10,y=50)
cat.config(text='Hi! I Am thinking a number between 1 and 10.')
btn = Button(win2,text='start',command=check)
btn.place(x=30, y=150)
btn2 = Button(win2, text='reset', command=reset)
btn2.place(x=110,y=150)
win2.mainloop()
def check():
user_try = int(txt.get())
if user_try < pc:
msg = 'Hmmmm... the number, which I thought of, is greater than this.'
elif user_try > pc:
msg = 'How about trying a smaller number ?!'
elif user_try == pc:
msg = 'Well Done! You guessed! It was %s the number!' % user_try
else:
msg = 'Something Went Wrong...'
cat['text'] = msg
win1 = Tk()
win1.title('First Window')
win1.geometry('350x300')
user = Label(win1,text='first window')
user.place(x=10,y=10)
btn1 = Button(win1,text='Open Window 2', command=openwin2)
btn1.place(x=10,y=50)
win1.mainloop()
There are multiple ways to do this in tkinter, here's a rework of your code using StringVar objects set to the textvariable properties of your Entry objects:
import tkinter as tk
def doit():
out_string.set("Hello " + in_string.get())
root = tk.Tk()
in_string = tk.StringVar()
out_string = tk.StringVar()
cat = tk.Entry(root, textvariable=in_string)
cat.place(x=20, y=25, width=100)
user = tk.Entry(root, textvariable=out_string)
user.place(x=20, y=75)
btn = tk.Button(root, text='START', command=doit)
btn.place(x=20, y=150)
root.mainloop()
Per #Mike-SMT, here's a different approach using Entry.get() and Entry.insert(). It augments the text when the user clicks the button:
import tkinter as tk
def doit():
user.insert(tk.END, cat.get())
root = tk.Tk()
cat = tk.Entry(root)
cat.place(x=20, y=25, width=100)
user = tk.Entry(root)
user.place(x=20, y=75)
user.insert(0, "Hello ")
btn = tk.Button(root, text='START', command=doit)
btn.place(x=20, y=150)
root.mainloop()
However, you'll see that subsequent button clicks keep appending the text. When working with Entry.insert(), you need to work with Entry.delete() and/or other Entry methods to properly manipulate the text.

How to create a tkinter toggle button?

I've been working on a text editor using Tkinter in Python 2.7.
A feature that I'm trying to implement is the Night Mode, where the user can toggle between a black background and a light one, that switches from light to dark with a click of the toggle button.
from Tkinter import *
from tkSimpleDialog import askstring
from tkFileDialog import asksaveasfilename
from tkFileDialog import askopenfilename
from tkMessageBox import askokcancel
Window = Tk()
Window.title("TekstEDIT")
index = 0
class Editor(ScrolledText):
Button(frm, text='Night-Mode', command=self.onNightMode).pack(side=LEFT)
def onNightMode(self):
if index:
self.text.config(font=('courier', 12, 'normal'), background='black', fg='green')
else:
self.text.config(font=('courier', 12, 'normal'))
index = not index
However, on running the code, it is always in the night mode and the toggle doesn't work. Help.
Source Code: http://ideone.com/IVJuxX
You can import tkinter library (Use capital letter for python 2.7):
import Tkinter
Create tkinter objects...
root = tk.Tk()
...and tkinter button
toggle_btn = tk.Button(text="Toggle", width=12, relief="raised")
toggle_btn.pack(pady=5)
root.mainloop()
Now create a new command button called "toggle" in order to create the effect of "toggle" when you press playing on the relief property (sunken or raised) :
def toggle():
if toggle_btn.config('relief')[-1] == 'sunken':
toggle_btn.config(relief="raised")
else:
toggle_btn.config(relief="sunken")
At the end apply this behaviour on your button:
toggle_btn = tk.Button(text="Toggle", width=12, relief="raised", command=toggle)
The background and fg are set only in the if-clause. You need to set them also in the else clause:
def onNightMode(self):
if index:
self.text.config(font=('courier', 12, 'normal'), background='black', fg='green')
else:
self.text.config(font=('courier', 12, 'normal'))
index = not index
i.e.,
else:
self.text.config(font=('courier', 12, 'normal'), background='green', fg='black')
Here's a code snippet that will help you with the toggle button animation if you would like to. You only need to add the functions that you want to execute when clicking of course, that's up to you.
'''
import tkinter as tk
# --- functions ---
def move(steps=10, distance=0.1):
if steps > 0:
# get current position
relx = float(frame.place_info()['relx'])
# set new position
frame.place_configure(relx=relx+distance)
# repeate it after 10ms
root.after(10, move, steps-1, distance)
def toggle(event):
if button["text"] == "Yes":
move(25, 0.02) # 50*0.02 = 1
button["text"] = "No"
print("Clicked on yes")
elif button["text"] == "No":
move(25, -0.02)
button["text"] = "Yes"
print("Clicked on no")
# --- main --
root = tk.Tk()
frame = tk.Frame(root, background='red')
frame.place(relx=0, rely=0, relwidth=0.5, relheight=1)
# to center label and button
#frame.grid_columnconfigure(0, weight=1)
#frame.grid_rowconfigure(0, weight=1)
#frame.grid_rowconfigure(3, weight=1)
button = tk.Button(frame, text='Yes',width=5,height=1)
button.place(relx=0.25,rely=0.5,relwidth=0.5, relheight=0.1)
button.bind("<Button-1>",toggle)
root.mainloop()
Albe's answer is good but it has some bad coding practices.
Following the same steps:
Import Tkinter as tk
top = tk.TK()
Define your function here and make it work for any button, not hard coded to the specific button you might use.
def toggle(button: tk.Button):
if button.config('relief')[-1] == 'sunken':
button.config(relief="raised")
else:
button.config(relief="sunken")
Then create and pack all the toggle buttons you want.
toggleButton = tk.Button(text="Toggle", width=12, relief="sunken",
command =lambda:toggle(toggleButton))
toggleButton.pack(pady=5)
top.mainloop()
This is better for two reasons. Creating the button object twice is redundant and will lead to buggy code. Hard coding the button to a specific toggle function is unscalable. This solution makes the code reusable and simple to add to. For example, replace that last block with:
for _ in range(4):
b = tk.Button(text="Toggle", width=12, relief="sunken")
b['command']= lambda a=b:toggle(a)
b.pack(pady=5)
And now you get 4 toggling buttons without any additional functions or copy/paste

Categories

Resources