Python 2.7 Tkinter redraw widget inside if - python

I have problem with my Tkinter code in Python 2.7. I have main window with one widget (button). I want to redraw window (and change value of variable -> add one widget) after click on the button. Where is problem? I think that problem can be, that every loop of mainloop change variable to 0. Thank you!
from Tkinter import *
def function():
global variable
variable = 0
main.update()
variable = 0
main = Tk() #New Tk window
if variable == 1:
Checkbutton(main, text="test").pack()
Button(main, text="Change", command=function).pack()
main.mainloop()

You never set variable to 1, and you should use functions (and classes) when working with GUIs.
from Tkinter import *
main = Tk() #New Tk window
variable = 0
def function():
global variable
variable = 1
newThing()
def newThing():
global variable
if variable==1:
Checkbutton(main, text="test").pack()
variable = 0
Button(main, text="Change", command=function).pack()
main.mainloop()

Related

Cant break loop using button

I wanted make window which will reappear if closed but will only close if user presses a button. I tried so many times but cant make. Plz help.
from Tkinter import *
x='y'
while x!='break':
def something(x):
x='break'
root=tkinter.Tk()
button=tkinter.Button(root, text='Break', command=lambda:something(x))
button.pack()
root.mainloop()
print('done')
When you set x using x = "break", you set the local variable which means that the global variable x is still "y". So to solve your problem just make sure that you use the global variable for x. This is a simple example:
import tkinter as tk
def something():
# Use the global variable for `loop_running`
global loop_running
loop_running = False
# You might also want to add: root.destroy()
loop_running = True
while loop_running:
root = tk.Tk()
button = tk.Button(root, text="Break", command=something)
button.pack()
root.mainloop()
print("Done")

Move two windows together tkinter

I have two types of windows: Main and Child. When I move main, all child windows must move also.
So I tried to write a method, but I am new to Tkinter so it is a bit hard. Isn't there a mehtod which Tkinter already provides?
There are two errors which occure:
line 21, in move_me if second_window != None:
NameError: name 'second_window' is not defined
wm_geometry() takes from 1 to 2 positional arguments but 3 were given
''' import tkinter as tk
from tkinter import *
from tkinter import Tk
from functools import partial
from tkinter import filedialog
import tkinter as tk
root=Tk()
def second_window_X():
global second_window
second_window=Tk()
label=Label(second_window, text='window')
label.pack()
button=Button(root, text='second window', command=second_window_X)
button.pack()
def move_me(event):
if second_window != None:
x = root.winfo_x()
y = root.winfo_y()
second_window.geometry(x,y)
root.bind("<Configure>", move_me)
root.mainloop()````
Is there someone who can give me an example how to link both windows togehter and make them move at the same time? And who can explain to me, why move me doesn't knows second_window even if i declared it as global?
Thank you very much already
Sorry for all the imports
As I suggested in a comment, you shouldn't have two instances of Tk in an application. Your second window should be an instance of Toplevel.
The below code moves the second window when the first window is moved/resized.
from tkinter import *
root=Tk()
second_window = None
def second_window_X():
global second_window
second_window=Toplevel(root)
label=Label(second_window, text='window')
label.pack()
button=Button(root, text='second window', command=second_window_X,width=100)
button.pack()
def move_me(event):
try:
if second_window != None:
x = root.winfo_x()
y = root.winfo_y()
second_window.geometry(f"+{x}+{y}")
except NameError:
pass
root.bind("<Configure>", move_me)
root.mainloop()

tkinter checkbutton not setting variable

Whatever I do to my checkbutton, it does not seem to set the variable.
Here's the parts of the code that are involved:
class Window:
def __init__(self):
self.manualb = 0 #to set the default value to 0
def setscreen(self):
#screen and other buttons and stuff set here but thats all working fine
manual = tkr.Checkbutton(master=self.root, variable=self.manualb, command=self.setMan, onvalue=0, offvalue=1) #tried with and without onvalue/offvalue, made no difference
manual.grid(row=1, column=6)
def setMan(self):
print(self.manualb)
#does some other unrelated stuff
It just keeps printing 0. Am I doing something wrong? Nothing else does anything to manual.
You're looking for IntVar()
IntVar() has a method called get() which will hold the value of the widget you assign it to.
In this particular instance, it will be either 1 or 0 (On or off).
You can use it something like this:
from tkinter import Button, Entry, Tk, Checkbutton, IntVar
class GUI:
def __init__(self):
self.root = Tk()
# The variable that will hold the value of the checkbox's state
self.value = IntVar()
self.checkbutton = Checkbutton(self.root, variable=self.value, command=self.onClicked)
self.checkbutton.pack()
def onClicked(self):
# calling IntVar.get() returns the state
# of the widget it is associated with
print(self.value.get())
app = GUI()
app.root.mainloop()
This is because you need to use one of tkinter's variable classes.
This would look something like the below:
from tkinter import *
root = Tk()
var = IntVar()
var.trace("w", lambda name, index, mode: print(var.get()))
Checkbutton(root, variable=var).pack()
root.mainloop()
Essentially IntVar() is a "container" (very loosely speaking) which "holds" the value of the widget it's assigned to.

Trying to print the output from my function inside my GUI window

Im trying to make a little program that endlessly prints out numbers inside GUI window, I can not find a way to print the out put of the function in a text box inside the GUI window instead of the python shell, please help, here is my code so far...
import sys
from tkinter import *
root = Tk()
def number(event):
x = 420
while True:
x +=420
print(x^70)
button_1 = Button(root, text="Start...")
button_1.bind("<Button-1>", number)
button_1.pack()
root.mainloop()
Thanks Harvey
You'll find it hard to constantly insert a value into a widget. The widget does not update with each insert. You can think of it has having a temporary variable for it. It can be accessed during the loop (as shown with print). However you'll notice that the widget itself doesn't update until the loop is over. So if you have while True then your widget will never update, and so you won't have the numbers streaming into the widget.
import sys
from tkinter import *
root = Tk()
def number():
x = 420
while x < 8400: # Limit for example
x +=420
textbox.insert(END, str(x^70)+'\n')
print(textbox.get(1.0, END)) # Print current contents
button_1 = Button(root, text="Start...", command=number) #Changed bind to command, bind is not really needed with a button
button_1.pack()
textbox = Text(root)
textbox.pack()
root.mainloop()

How do I create a button in Python Tkinter to increase integer variable by 1 and display that variable?

I am trying to create a Tkinter program that will store an int variable, and increase that int variable by 1 each time I click a button, and then display the variable so I can see that it starts out as 0, and then each time I click the button it goes up by 1. I am using python 3.4.
import sys
import math
from tkinter import *
root = Tk()
root.geometry("200x200")
root.title("My Button Increaser")
counter = 0
def nClick():
counter + 1
def main_click():
mLabel = Label(root, text = nClick).pack()
mButton1 = Button(text = "Increase", command = main_click, fg = "dark green", bg = "white").pack()
root.mainloop()
Ok so there are a few things wrong with your code so far. My answer basically changes what you have already into the easiest way for it to do what you want.
Firstly you import libraries that you don't need/use (you may need them in your whole code, but for this question include a minimal example only). Next you must deifne the counter variable as a global variable, so that it will be the same in the function (do this inside the function as well). Also you must change counter + 1 to counter += 1 so it increments the variable
Now the code will be able to count, but you have set variables as Buttons, but then made them None type objects, to change this .pack() the variable on the next line. You can get rid of the second function as you only need one, then you change the command of the button and its text to counter. Now to update the text in the button, you use .config(text = counter) which configures the button.
Here is the final solution (changes button value and has no label, but this is easily changed):
from tkinter import *
root = Tk()
root.geometry("200x200")
root.title("My Button Increaser")
global counter
counter = 0
def nClick():
global counter
counter += 1
mButton1.config(text = counter)
mButton1 = Button(text = counter, command = nClick, fg = "darkgreen", bg = "white")
mButton1.pack()
root.mainloop()
hope that helps!
You could use Tkinter variables. They are specially useful when you need to modify a data that other widgets might interact with. Here is a look alike code to the one in the question, but instead of defining counter as a normal variable, it is a variable from Tkinter.
import tkinter
import sys
root = tkinter.Tk()
root.geometry("200x200")
root.title("His Button Increaser")
counter = tkinter.IntVar()
def onClick(event=None):
counter.set(counter.get() + 1)
tkinter.Label(root, textvariable=counter).pack()
tkinter.Button(root, text="Increase", command=onClick, fg="dark green", bg = "white").pack()
root.mainloop()
Instead of passing the value this variable holds to the text attribute of the Label, we assign the variable to textvariable attribute, so when the value of the variable gets updated, Label would update the displayed text accordingly.
When you want to change the value of the variable, you'd need to call the set() method of the variable object (see onClick) instead of assigning the value directly to it.

Categories

Resources