from Tkinter import *
window = Tk()
frame=Frame(window)
frame.pack()
text_area = Text(frame)
text_area.pack()
text1 = text_area.get('0.0',END)
def cipher(data):
As,Ts,Cs,Gs, = 0,0,0,0
for x in data:
if 'A' == x:
As+=1
elif x == 'T':
Ts+=1
elif x =='C':
Cs+=1
elif x == 'G':
Gs+=1
result = StringVar()
result.set('Num As: '+str(As)+' Num of Ts: '+str(Ts)+' Num Cs: '+str(Cs)+' Num Gs: '+str(Gs))
label=Label(window,textvariable=result)
label.pack()
button=Button(window,text="Count", command= cipher(text1))
button.pack()
window.mainloop()
What I'm trying to accomplish is entering a string of 'AAAATTTCA' in my Text widget and have the label return the number of occurrences. With the entry 'ATC' the function would return Num As: 1 Num Ts: 1 Num Cs: 1 Num Gs: 0.
What I don't understand is why I am not reading in my text_area correctly.
I think you misunderstand some concepts of Python an Tkinter.
When you create the Button, command should be a reference to a function, i.e. the function name without the (). Actually, you call the cipher function once, at the creation of the button. You cannot pass arguments to that function. You need to use global variables (or better, to encapsulate this into a class).
When you want to modify the Label, you only need to set the StringVar. Actually, your code creates a new label each time cipher is called.
See code below for a working example:
from Tkinter import *
def cipher():
data = text_area.get("1.0",END)
As,Ts,Cs,Gs, = 0,0,0,0
for x in data:
if 'A' == x:
As+=1
elif x == 'T':
Ts+=1
elif x =='C':
Cs+=1
elif x == 'G':
Gs+=1
result.set('Num As: '+str(As)+' Num of Ts: '+str(Ts)+' Num Cs: '+str(Cs)+' Num Gs: '+str(Gs))
window = Tk()
frame=Frame(window)
frame.pack()
text_area = Text(frame)
text_area.pack()
result = StringVar()
result.set('Num As: 0 Num of Ts: 0 Num Cs: 0 Num Gs: 0')
label=Label(window,textvariable=result)
label.pack()
button=Button(window,text="Count", command=cipher)
button.pack()
window.mainloop()
Related
How to make a button with a command with the parameter/attribute of its name with tkinter?
from tkinter import *
def openfile(name):
print(name)
for i in l:
if num == 2:
num = 0
y = y+100
x = 100
print(y)
print(x)
print(num)
bt = Button(second_frame, text=i, command=lambda: openfile(i)).grid(row=y, column=x, pady=10, padx=10)
num = num + 1
x = x + 100
print(num)
I want to set the button command to be open file then its name as an attribute. I don't know how to get the buttons name on the same line.
NOTE:
The button name differs from each button
You mean something like this ?
for i in range(5):
btn_name=str(i)
btn_obj=Button(second_frame,text=btn_name,command=lambda name=btn_name:openfile(name))
I am trying to reuse the action of this button, without recalling the command all over again, the thing is that, after the button executes the first "if" statement "y == 1".
Now, instead of having access to the second "if" statement "y == 2" (That is, assume the program starts now, if I enter 1 in the entry box and the button is clicked, the program should print "Yes!", then if I enter 2 again in the entry box and the button is clicked, the program should print "Yes!Yes!", but instead it starts the "def action()" all over again)
I want it to run like the second code if I use a console
from tkinter import *
win = Tk()
def action():
y = x.get()
if y == 1:
print("Yes!")
if y == 2:
print("Yes!Yes!")
elif y == 3:
print("Yes!Yes!Yes!")
else:
print("No")
x = IntVar()
e1 = Entry(win, textvariable = x).grid()
b1 = Button(win, text = "Button", command = action).grid()
win.mainloop()
The second code
y = eval(input("Enter a value: "))
if y == 1:
print("Yes")
y = eval(input("Enter a value: "))
if y == 2:
print("Yes!Yes!")
elif y == 3:
print("Yes!Yes!Yes!")
else:
print("No")
put
y = x.get()
clicked = False
after
b1 = Button(win, text = "Button", command = action).grid()
Now,
def action():
if y == 2 and clicked == True:
print("Yes!Yes!")
if y == 3 and clicked == True:
print("Yes!Yes!Yes!")
if y == 1 and clicked == False:
print("Yes!")
clicked = True
If I understood your question well, this might pop the desired result.
Walrus to rescued.
Code:
from tkinter import *
win = Tk()
def action():
# y = x.get()
if (y := x.get()) == 1:
print("Yes!")
elif y == 2:
txt= 'Yes!'
print(txt*2)
elif y == 3:
txt = 'Yes!'
print(txt*3)
else:
print("No")
x = IntVar()
e1 = Entry(win, textvariable = x).grid()
b1 = Button(win, text = "Button", command = action).grid()
win.mainloop()
Result:
Yes!
Yes!Yes!
Yes!Yes!Yes!
No
if I enter 1 in the entry box and the button is clicked, the program
should print "Yes!", then if I enter 2 again in the entry box and the
button is clicked, the program should print "Yes!Yes!", and so on.
Walrus to rescued.
Code:
from tkinter import *
win = Tk()
def action():
# y = x.get()
if (y := x.get()) == 1:
print("Yes!")
elif y == 2:
txt= 'Yes!'
print(txt*2)
elif y == 3:
txt = 'Yes!'
print(txt*3)
else:
print("No")
x = IntVar()
e1 = Entry(win, textvariable = x).grid()
b1 = Button(win, text = "Button", command = action).grid()
win.mainloop()
Result:
Yes!
Yes!Yes!
Yes!Yes!Yes!
No
My variable is not changing and I know it's not changing because "1" is printed to the console.
I'm trying to make the label increment when i press the button. However when I press the button, the variable stays at 1.
What am I doing wrong?
I've looked online for an answer but I cannot really find one that I can understand.
num = 0
import tkinter
box = tkinter.Tk()
v = tkinter.StringVar()
labels = tkinter.Label(box, textvariable = v)
labels.pack()
def numberz(num,v):
num += 1
v.set(num)
print(num)
class MainWindow():
box.title("My Stupid Program")
buddon = tkinter.Button(box, text='PRESS ME', command = lambda:numberz(num,v))
buddon.pack()
box.mainloop()
num = 0
import tkinter
box = tkinter.Tk()
v = tkinter.StringVar()
labels = tkinter.Label(box, textvariable = v)
labels.pack()
def numberz(num,v):
num += 1
v.set(num)
print(num)
class MainWindow():
box.title("My Stupid Program")
buddon = tkinter.Button(box, text='PRESS ME', command = lambda:numberz(num,v))
buddon.pack()
box.mainloop()
You are changing the parameter num and not the global variable num
To change the global you need to specifically reference it. Notice how num is not passed in the lambda and now there is a global num in you function.
num = 0
import tkinter
box = tkinter.Tk()
v = tkinter.StringVar()
labels = tkinter.Label(box, textvariable = v)
labels.pack()
def numberz(v):
global num
num += 1
v.set(num)
print(num)
class MainWindow():
box.title("My Stupid Program")
buddon = tkinter.Button(box, text='PRESS ME', command = lambda:numberz(v))
buddon.pack()
box.mainloop()
In any case, using globals should be restricted to very specific cases and not be of general use.
I'm trying to write simple GUI that will generate numbers, write them to the string, and after pressing "Check" button it will compare the input from the user with generated numbers.In this code function print(x==y) should print me TRUE, since i provide the same numbers which were generated, but it prints me FALSE.What is the problem? Could you help me please?
from tkinter import*
from random import randint
def compare():
global string2
global string1
string2=str(string2.get())
print (string1 == string2)
def generate():
global k
number.config(text=string1[k])
k=k+1
number.after(2000, remove)
def remove():
global repeat
number.config(text='')
repeat -= 1
if repeat > 0:
number.after(1000, generate)
root = Tk()
i=3
k=0
j=0
repeat = i
string1=str()
string2=StringVar()
while j < i:
string1=string1+str(randint(1,10))
j=j+1
number = Label(root, width=20, height=10)
number.pack()
ent = Entry(root,width=20,bd=3)
ent.pack()
but = Button(text="Check",command = compare)
but.pack()
generate()
root.mainloop()
The main problem is ranint(1,10) which can give values 1...10,not 1...9.
So finally it generates 4-digits number instead of 3-digits one.
(but it could generate even 6-digits number "101010" - "10" + "10" + "10")
Use randint(1, 9) or randrange(1, 10)
import tkinter as tk
from random import randint
# --- functions ---
def compare():
print(number_str == answer.get())
def generate():
global index
lab.config(text=number_str[index])
index += 1
root.after(2000, remove)
def remove():
global repeat
lab.config(text='')
repeat -= 1
if repeat > 0:
root.after(1000, generate)
# --- main ---
number_of_digits = 3
repeat = number_of_digits
index = 0
number_str = ""
for _ in range(number_of_digits):
number_str = number_str + str(randint(1,9))
#print(number_str)
root = tk.Tk()
answer = tk.StringVar()
lab = tk.Label(root, width=20, height=10)
lab.pack()
ent = tk.Entry(root, textvariable=answer, width=20, bd=3)
ent.pack()
but = tk.Button(text="Check", command=compare)
but.pack()
generate()
root.mainloop()
import sys
from tkinter import *
def main():
mtext = ment.get()
mlabel2 = Label(top, text=mtext).pack()
def isbn():
digits = [(11 - i) * num for i, num in enumerate(map(int, list()))]
digit_11 = 11 - (sum(digits) % 11)
if digit_11 == 10:
digit_11 = 'X'
digits.append(digit_11)
isbn_number = "".join(map(str, digits))
label2 = Label(top, text = "Your ISBN number is",).pack()
top = Tk()
top.geometry("450x450+500+300")
top.title("Test")
button = Button(top,text="OK", command = isbn, fg = "red", bg="blue").pack()
label = Label(top, text = "Please enter the 10 digit number").pack()
ment= IntVar()
mEntry = Entry(top,textvariable=ment).pack()
Hello, I the code at the moment is a working stage, I just need a way of the results printing because at the moment it is not. I would also like the converter to work proper ally with 10 digits and the 11th digit being the number that the converter finds outs. Thanls
Note how the button's command calls the function to perform your operation:
from tkinter import *
def find_isbn(isbn, lbl):
if len(isbn) == 10:
mult = 11
total = 0
for i in range(len(isbn)):
total += int(isbn[i]) * mult
mult -= 1
digit_11 = 11 - (total % 11)
if digit_11 == 10:
digit_11 = 'X'
isbn += str(digit_11)
lbl.config(text=isbn)
top = Tk()
top.geometry("450x450+500+300")
top.title("Test")
button = Button(top, text="OK", command=lambda: find_isbn(mEntry.get(), mlabel2), fg="red", bg="blue")
button.pack()
label = Label(top, text="Please enter the 10 digit number")
label.pack()
mEntry = Entry(top)
mEntry.pack()
mlabel2 = Label(top, text='')
mlabel2.pack()
top.mainloop()
You also need to call .mainloop(), on your master widget to get the whole things started. It's also better to call .pack() on an object on a separate line. pack() and grid() return nothing so you won't be able to use other methods on the object once you do that.