Quite new in Python. I am trying to create an interface with different radiobuttons. Each radiobutton gets a dataset. Two buttons that when are clicked recall two different functions: find matches and mismatches between the datasets selected by the radiobuttons. the result is printed in a widget. I really cannot get to work the functions of matching and mismatching. Can I have help to know how to proceed please? Best,
**from tkinter import *
from tkinter import messagebox
root = Tk()
root.geometry('400x400')
root.title("matches and mismatches")
def matches():
messagebox.showinfo("function of finding matches")
def mismatches():
messagebox.showinfo("function of finding mismatches")
Datasetone = ["Michel", "John", "Carol", "Bob", "Justine", 'Harry',]
Datasettwo = ["Justine", "John", "Carol", "Bob", "Josh", "Marcello"]
Radiobutton(root, text='Dataset one').pack()
Radiobutton(root, text='Dataset two').pack()
Matches = Button(root, text='Matches', command=matches).pack()
Mismatches = Button(root, text='Mismatches', command=mismatches).pack()
root.mainloop()**
CodePudding user response:
here is example code of how it can be done. It is simple so you can understand it better.
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.geometry('400x400')
root.title("matches and mismatches")
var = tk.IntVar()
var2 = tk.IntVar()
data_set_one = ["Michel", "John", "Carol", "Bob", "Justine", "Harry"]
data_set_two = ["Justine", "John", "Carol", "Bob", "Josh", "Marcello"]
dictionary = [{"id": 1, "data_set": data_set_one}, {"id": 2, "data_set": data_set_two}]
def matches(set1, set2):
for item in dictionary:
if item["id"] == set1.get():
first_set = item["data_set"]
elif item["id"] == set2.get():
second_set = item["data_set"]
else:
continue
match = []
for k in first_set:
for v in second_set:
if k == v:
match.append(k ',')
messagebox.showinfo("Matches", message=match)
def mismatches(set1, set2):
for item in dictionary:
if item["id"] == set1.get():
first_set = item["data_set"]
elif item["id"] == set2.get():
second_set = item["data_set"]
else:
continue
mismatch = []
for k in first_set:
if k not in second_set:
mismatch.append(k ',')
messagebox.showinfo("Mismatches", message=mismatch)
tk.Radiobutton(root, text='Dataset one', variable=var, value=1).pack()
tk.Radiobutton(root, text='Dataset two', variable=var2, value=2).pack()
Matches = tk.Button(root, text='Matches', command=lambda: matches(var, var2)).pack(pady=5)
Mismatches = tk.Button(root, text='Mismatches', command=lambda: mismatches(var, var2)).pack(pady=5)
root.mainloop()
Import is changed from from tkinter import *
to import tkinter as tk
.
Two variables var
and var2
are created and they are int
type. After list of dictionary
is created where every data_set
is given its id
. In radiobuttons
, variable
and value
is added. (for every dataset there is tk.IntVar()
and value is equal to id
inside dictionary
). Two functions, mismatches
and matches
are created and both functions have two parameters
.
Inside function matches
we check if id
inside dictionary
is equal to selected radiobutton
, and if it is, variable first_set
is assigned value of dictionary
for that item. Empty list match
is created and inside two loops
we check if two list have matching items, and if there is matching item, it is added to list match
. In messagebox
title is set and message is equal to match
. In mismatches
function everything is similar to matches
function. In mismatches
2nd way of getting matches/mismatches is displayed in for loop
.
CodePudding user response:
var = tk.IntVar()
var2 = tk.IntVar()
var4 =tk.IntVar()
data_set_one = ["Michel", "John", "Carol", "Bob", "Justine", "Harry"]
data_set_two = ["Justine", "John", "Carol", "Bob", "Josh", "Marcello"]
data_set_three = ["Anna", "Tom", "Carol", "Bob", "Franco", "Marcello"]
dictionary = [{"id": 1, "data_set": data_set_one}, {"id": 2, "data_set": data_set_two}, {"id": 3, "data_set": data_set_three}]
def matches(set1, set2, set3):
for item in dictionary:
if item["id"] == set1.get():
first_set = item["data_set"]
elif item["id"] == set2.get():
second_set = item["data_set"]
elif item["id"] == set3.get():
third_set = item["data_set"]
else:
continue
match = []
for k in first_set:
for v in second_set:
for s in third_set:
if k == v:
elif s == k:
match.append(k ',')
messagebox.showinfo("Matches", message=match)
tk.Radiobutton(root, text='Dataset one', variable=var, value=1).pack()
tk.Radiobutton(root, text='Dataset two', variable=var2, value=2).pack()
tk.Radiobutton(root, text='Dataset three', variable=var4, value=4).pack()
Matches = tk.Button(root, text='Matches', command=lambda: matches(var, var2, var4)).pack(pady=5)
root.mainloop()